| View previous topic :: View next topic |
| Author |
Message |
calguy1000 Smarty Rookie
Joined: 27 Mar 2009 Posts: 22
|
Posted: Sun Jan 29, 2012 11:07 pm Post subject: Template Inheritance - Get Parsed Template |
|
|
Howdy.
I have a question regarding template inheritance:
I am working with Smarty3 in CMSMS. and in various places in the code we need to be able to parse a template to extract information from certain tags. i.e: {content block=foo label='whatever'} so that we can provide an input field or a text area, etc in an admin interface so the editor can provide some content for that block.
We cannot (we dont think) simply parse the template through smarty in our admin interface and provide a different plugin handler because of situations like this, which may fail in the admin interface:
{if $current_hour < 12}{content block='morning'}{else}{content block='afternoon'}{/if}
The expression could cause one of the {content} blocks to not be called, and therefore I would not be able to build a text area for those content blocks, for the content editor to be able to provide some text.
In smarty2 we handled this with just simple regular expressions, and a few limitations (not supporting {include} etc). In smarty3 with template inheritance this is even more complex.
My question is. How could I get the smarty text for the complete 'merged' template, including any {include} or {extends}/{block} stuff... from smarty, but without going through the compilation/parsing stage?
Or, if anybody has a better idea? |
|
| Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4177 Location: Hamburg / Germany
|
Posted: Tue Jan 31, 2012 4:45 pm Post subject: |
|
|
When you are using template inheritance {block} tags of the chid templates get merged during the compilation of the parent template.
There is no way to get access to the whole merged source, but all source is running through optional prefilters.
But maybe you found the answer already in our private email exchange.... |
|
| Back to top |
|
calguy1000 Smarty Rookie
Joined: 27 Mar 2009 Posts: 22
|
Posted: Thu Feb 02, 2012 4:09 pm Post subject: |
|
|
Actually, I had an epiphany.
When I need to 'parse' the template during compilation to have a callback on all tags of a specific type, I can just implement the tag as a compiler tag.
i.e: $smarty->registerPlugin('compiler','content',$some_callback);
Then the callback should/will be called during the compile phase, independent of logic, and i can extract the attributes passed to the tag etc.
So the question is (just to confirm) will my 'compiler' tag be called independent of logic surrounding it in the template? |
|
| Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4177 Location: Hamburg / Germany
|
Posted: Thu Feb 02, 2012 4:15 pm Post subject: |
|
|
| Yes your compiler callback will be called for each occurance in the source. This does also apply for child blocks of template inheritance. |
|
| Back to top |
|
rodneyrehm Administrator

Joined: 30 Mar 2007 Posts: 698 Location: Germany, border to Switzerland
|
Posted: Thu Feb 02, 2012 5:15 pm Post subject: |
|
|
Uwe, what about Smarty_Internal_Template::getTags()? _________________ Twitter |
|
| Back to top |
|
calguy1000 Smarty Rookie
Joined: 27 Mar 2009 Posts: 22
|
Posted: Sun Apr 08, 2012 3:27 pm Post subject: Followup: Detecting which block a tag is in. |
|
|
Given this scenario:
template:test_parent (template is a resource)
| Code: | {block name='block1'}{content}{/block}
{block name='block2'}{content block='somename'}{/block}
|
template:test_child
| Code: | {extends file='template:test_parent'}
{block name='block2'}{content block='someothername'}{/block}
|
template:test_child2
| Code: | {extends file='template:test_parent'}
{block name='block2'}{content block='yetanothername'}{/block}
|
I have a compiler tag that is successfully finding ALL of the content blocks, and allowing me to build a list of them, and their attributes, so that I can build a form with text areas representing the various {content} blocks. This is great.
However, when editing a page that uses the test_child template, (multiple content pages can use the same smarty template). I only want the text area representing the {content} and {content block='someothername'} tags to show up in my form because {block name='block2'} has been replaced. Similarly when editing a page that uses the test_child2 template I want to see text areas representing {content} and {content block='yetanothername'}.
Is there some way, in my custom compiler tag to know the 'scope' when that compiler tag is called. i.e: if I know that I am within the 'block2' scope in a child template, I can hopefully do something to remove from my list all of the detected content tags from the same block in any parent template(s).
So ideally I need to know the current template name (during compilation) and it's inheritance tree... and the block name that is currently being compiled (if any).
Thanks in advance for any help you can give. |
|
| Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 4177 Location: Hamburg / Germany
|
Posted: Sun Apr 08, 2012 5:59 pm Post subject: |
|
|
It's not possible to obtain the 'scope' in which the compiler plugin was called.
Could you not add this information as additional parameter when calling the plugin? |
|
| Back to top |
|
|