DooPHP IRC channel


Template : DooViewBasic

Reference Guides for using specific components of DooPHP

Meta Data Namespace

Postby RichardM » Mon Feb 01, 2010 12:14 pm

Meta Data Namespace
If you do not want your 'for' loop meta data being assigned to doo.for.foo.first and would rather have it in myapp.for.foo.first you can set the config option in common.conf.php as follows
Code: Select all
$config['APP_NAMESPACE'] = 'myapp';


Then in your templates you would replace
Code: Select all
{% for foo as bar with meta %}
 {{doo.for.bar.index}}
{% endfor %}


With
Code: Select all
{% for foo as bar with meta %}
 {{myapp.for.bar.index}}
{% endfor %}


If you have your own render function defined in your base controller you might like to do something like
Code: Select all
class BaseController extends DooController {
private $data = array();
public function render($view) {
$this->data[Doo::conf()->APP_NAMESPACE]['params'] = $this->params;
$this->view()->render($view, $this->data);
}
}
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Filters: foo.bar|default(0)

Postby RichardM » Tue Feb 16, 2010 1:59 pm

Filters: foo.bar|default(0)
I have now added support for 'filters' in DooView Basic. This allows you to place a pipe "|" after a variable, function or array and the item to the left will be passed as the first argument to the filter on the right. This is best explained with some simple examples:
Code: Select all
{{foo|upper}}
{{foo.bar|upper}}
{{foo|default(1)}}
{{foo|default('Welcome')|upper}}
{% if foo|upper == "HI" %}


As you can see above the filters can be chained one after another and also have additional parameters passed into them. The filters should be defined in the TemplateTags class as 'filter_[name]' where [name] is the name of the filter. As you can see the filters all have a prefix appended to them.

The above examples will compile down to:
Code: Select all
<?php echo filter_upper($data['foo']); ?>
<?php echo filter_upper($data['foo']['bar']); ?>
<?php echo filter_default($data['foo'],1); ?>
<?php echo filter_upper(filter_default($data['foo'],'Welcome')); ?>
<?php if(filter_upper($data['foo'])=="HI"): ?>


If you do not want to have the filter_ prefix or want to change it for something else you can do
Code: Select all
$this->view()->setFilterFunctionPrefix('my_filter_');
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Safe Variable Access

Postby RichardM » Tue Feb 16, 2010 2:07 pm

Safe Variable Access
I have decided to add further support for ensuring your variable access is safe. The current setup only protects against PHP Warnings when $data['index'] is not defined. It also checks against foo.bar.0.xyz too. It does not however protect against foo.bar->method and foo.bar->property right now. I will consider adding this in later.

By default DooViewBasic does not enable this safe mode for backward compatibility. Therefor to enable it you need to do the following:
Code: Select all
$this->view()->enableSafeVariableAccess();
// OR to turn off
$this->view()->enableSafeVariableAccess(false);
// Or to change the default return type - Default is NULL
$this->view()->enableSafeVariableAccess(true, false);


If the safe option is enabled then your compiled variables in the forms of:
Code: Select all
{{foo}}
{{foo.bar}}
{% if foo.bar !== null %}


will convert down to:
Code: Select all
<?php echo DooViewBasic::is_set_or($data['foo']); ?>
<?php echo DooViewBasic::is_set_or($data['foo']['bar']); ?>
<?php if(DooViewBasic::is_set_or($data['foo']['bar'])!==null): ?>
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Cycle - Cycle over an array of elements

Postby RichardM » Sun Feb 28, 2010 2:46 am

Cycle - Cycle over an array of elements

If you want to iterate over an array of values within a loop you can now use the cycle statement. This is useful when you want to colour code alternate rows of a table. There are two ways in which you can use the cycle statement:

Code: Select all
{% for a from 1 to 10 %}
   {{a}} : {% cycle ['a', 'b', 'c'] %}<br />
{% endfor %}

{% set cycleElements as ['a', 'b', 'c', 'd'] %}
{% for a from 1 to 10 %}
   {{a}} : {% cycle using cycleElements %}<br />
{% endfor %}


The first lets you assign the array within the call to cycle while the other lets you make use of an already defined array of values. The two examples will output the following when called:

Code: Select all
1 : a
2 : b
3 : c
4 : a
5 : b
6 : c
7 : a
8 : b
9 : c
10 : a
1 : a
2 : b
3 : c
4 : d
5 : a
6 : b
7 : c
8 : d
9 : a
10 : b


The compiled versions of the code will look similar to this:
Code: Select all
// Using inline array
<?php $doo_view_basic_1267324709 = range(1, 10, 1);
if (!empty($doo_view_basic_1267324709)):
foreach($doo_view_basic_1267324709 as $data['a']):
?>
<?php echo DooViewBasic::is_set_or($data['a']); ?> :

// Cycle starts here
<?php
if (!isset($doo_view_basic_1267324710))
   $doo_view_basic_1267324710 = array('a','b','c');
echo current($doo_view_basic_1267324710);
if (next($doo_view_basic_1267324710)===false)
   reset($doo_view_basic_1267324710);
?>
// Cycle ends here

<br />
<?php endforeach;
 endif; ?>

// Using a preset array for cycle
<?php $data['cycleElements'] = array('a','b','c','d'); ?>
<?php $doo_view_basic_1267324711 = range(1, 10, 1);
if (!empty($doo_view_basic_1267324711)):
foreach($doo_view_basic_1267324711 as $data['a']):
?>
<?php echo DooViewBasic::is_set_or($data['a']); ?> :

// Cycle starts here
<?php
echo current($data['cycleElements']);
if (next($data['cycleElements'])===false)
   reset($data['cycleElements']);
?>
// End cycle here
<br />
<?php endforeach;
 endif; ?>


Note that cycle will always assume that the array to be cycled exists (safe mode does not work here as the next, current and reset functions do not work if it is). From a performance perspective its faster if you can define your array outside of the loop and make use of the 'using' keyword.

For a better example usage see the original request for this feature. Please note that the compiled code is different to that presented in the original request. viewtopic.php?f=9&t=1068&start=0
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Insert a template within a template

Postby RichardM » Sun Feb 28, 2010 3:17 am

Insert a template within a template
If you want to insert a template within a template as opposed to calling in such a template using the include tag you can now use the insert tag. This works in the same way as the include tag except that rather than compiling down to a PHP Include statement it simply reads the compiled template directly into the current template. Therefore if you have two templates
templateA.html
Code: Select all
{% insert 'templateB' %}


templateB.html
Code: Select all
I am the content of template B


Then the compiled version of templateA will be
Code: Select all
I am the content of template B


You can find some more information on this tag in the original feature request found here: viewtopic.php?f=7&t=1080
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Include using dynamic variables

Postby RichardM » Tue Jun 29, 2010 3:01 pm

Include Using dynamic variables
If you want to do an include using a dynamic variable for example you might want to have a 'success' and 'failure' view included into some space on a page you can not do this. Start by creating your 3 templates (main template, dynamicInclude1 and dynamicInclude2)
example.html
Code: Select all
{% include task_status %}


success.html
Code: Select all
Success: Did something


fail.html
Code: Select all
Failed: Failed to do something


Now in your controller set the $data variable your using in your template
Code: Select all
// Set the data
$data['task_status'] = 'success'; // 'fail';
// Now render your view using your preferred method render(...) or renderLayout(...)


This will then compile down so the example.html pages compiled template looks like so:
Code: Select all
<?php
   if(DooViewBasic::is_set_or($data['task_status']) != DooViewBasic::$safeVariableResult){
      $doo_view_basic_1277823181 = $this->compileSubView(DooViewBasic::is_set_or($data['task_status']));
      if ($doo_view_basic_1277823181[0] == true) {
         include $doo_view_basic_1277823181[1];
      } else {
         echo $doo_view_basic_1277823181[1];
      }
   } else {
      echo '<span style="color:#ff0000">task_status is not set</span>';
   }
?>


This will therefore cause the page to be recompiled if required.

If you know the template you want to use will not change then use
Code: Select all
{% include 'some_template' %}
Note: code samples my not be 100% accurate.
RichardM
 
Posts: 1329
Joined: Sun Aug 30, 2009 6:03 pm
Location: Cumbria, UK

Previous

Return to References

Who is online

Users browsing this forum: No registered users and 0 guests

cron