Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Multiple passes

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Sat Nov 05, 2011 11:41 am    Post subject: Multiple passes Reply with quote

I'd like to combine Smarty templates and separate translation files. But I'd like the translation files to include Smarty syntax itself as well.

For example a fragment of a template:
Code:
<div id="page">{$L10n_page}</div>


And translation string for $L10n_page:
Code:
{if $page_count gt 1}Page {$page_index} of {$page_count}{else}Single page only{/if}.


Result should be something like:
Code:
<div id="page">Page 3 of 10.</div>


I don't think it'll go any deeper than this one level.

Can I do this with Smarty3 and what would be best practice?

I'm interrested in any suggestion, including any that produce the same result but uses a different way to get there. The system I'm setting up is completely new, so I'm not bound to any legacy code.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Nov 05, 2011 1:13 pm    Post subject: Reply with quote

The best solution is to use template inheritance.

See http://www.smarty.net/inheritance and http://www.smarty.net/docs/en/advanced.features.template.inheritance.tpl

You can place all language dependent code in a child template blocks which are referenced by the language independent parent (main) template.

Example

parent.tpl
Code:

...
{block name='page'}{/block}
....


child_en.tpl
Code:

{block name='page'}
<div id="page">{if $page_count gt 1}Page {$page_index} of {$page_count}{else}Single page only{/if}</div>
{/block}



Then call it with
$smarty->display('extends:parent.tpl|child_en.tpl');

You can make the child template name variable for the different language templates.
Back to top
View user's profile Send private message
mwvdlee
Smarty Rookie


Joined: 05 Nov 2011
Posts: 7

PostPosted: Mon Nov 14, 2011 12:08 pm    Post subject: Reply with quote

Thanks for the inheritance suggestion, I would have never thought of inheritance this way.

But instead I discovered prefilters in combination with the template_id, which seem to do the job and is indeed advertised by the manual as a way to handle translations. It does require the system to clear cached results whenever a language file changes, but that's hardly an issue.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    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