Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Template Inheritance - Get Parsed Template

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
calguy1000
Smarty Rookie


Joined: 27 Mar 2009
Posts: 22

PostPosted: Sun Jan 29, 2012 11:07 pm    Post subject: Template Inheritance - Get Parsed Template Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4199
Location: Hamburg / Germany

PostPosted: Tue Jan 31, 2012 4:45 pm    Post subject: Reply with quote

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
View user's profile Send private message
calguy1000
Smarty Rookie


Joined: 27 Mar 2009
Posts: 22

PostPosted: Thu Feb 02, 2012 4:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4199
Location: Hamburg / Germany

PostPosted: Thu Feb 02, 2012 4:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Thu Feb 02, 2012 5:15 pm    Post subject: Reply with quote

Uwe, what about Smarty_Internal_Template::getTags()?
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
calguy1000
Smarty Rookie


Joined: 27 Mar 2009
Posts: 22

PostPosted: Sun Apr 08, 2012 3:27 pm    Post subject: Followup: Detecting which block a tag is in. Reply with quote

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
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4199
Location: Hamburg / Germany

PostPosted: Sun Apr 08, 2012 5:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP