This is useful when you'd like use the same HTML block(s) in several different locations in your template.
Syntax
To register a partial simply wrap your code in a v:partial tag:
<v:partial id="product">
<v:img path="image" />
<v:if path="price">
<div class="price"><v:text path="price" /></div>
</v:if>
<div class="name" title="<v:text path="name" />"><v:text path="name" /></div>
</v:partial>
<div class="featured product">
<v:partial id="product" path="/products[price > 0]" />
</div>
<ul class="products">
<li><v:partial id="product" path="/products" /></li>
</ul>
function partial($attributes, $tag, $context, $callback) {
global $partials;
$id = $attributes['id'];
if (count($tag['tags']) > 0) {
if (!is_array($partials)) {
$partials = array();
}
$partials[$id] = $tag;
} else {
if (array_key_exists('path', $attributes)) {
$context = vae($attributes['path']);
}
return vae_render_tags($partials[$id], $context);
}
}
vae_register_tag('partial', array(
'handler' => 'partial',
'params' => 'collection',
'required' => array('id')
));
Very cool to see a working example of building a custom vae tag.
Can't the same thing be accomplished though using templates?
in partial.html:<v:img path="image" />
<v:if path="price">
<div class="price"><v:text path="price" /></div>
</v:if>
<div class="name" title="<v:text path="name" />"><v:text path="name" /></div>
... and then to create the context context (section or collection:
<div class="featured product">
<section path="/products[price > 0]">
<v:template filename="/includes/partial.html"></v:template>
</v:section>
</div>
<ul class="products">
<v:collection path="/products">
<li>
<v:template filename="/includes/partial.html"></v:template>
</li>
</v:collection>
</ul>
It would be great to see if either has a speed advantage to the other.
It looks like you're new here. If you want to get involved, click one of these buttons!