|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mwvdlee Smarty Rookie
Joined: 05 Nov 2011 Posts: 7
|
Posted: Sat Nov 05, 2011 11:41 am Post subject: Multiple passes |
|
|
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 |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Sat Nov 05, 2011 1:13 pm Post subject: |
|
|
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 |
|
mwvdlee Smarty Rookie
Joined: 05 Nov 2011 Posts: 7
|
Posted: Mon Nov 14, 2011 12:08 pm Post subject: |
|
|
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 |
|
|
|
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
|