Dynamic content with PHP & Smarty

I'm using Smarty and utilizing the config_load respective {#VAR#} variables to implement localization. This is working perfectly fine as long as the content is within the templates, but fails as soon as I have to add dynamic content within the TPL file, i.e. with:

{if isset($var) } {foreach from=$var item=line} {$line}<br> {/foreach} {/if}

Please note each entry within $var contains usually one {#VAR#} entry - and they are not translated (the user will see {#VAR#}).

What is the proper way to implement localization in this case?



Solution

I ended up by only replacing {$line}<br> in the code above with:

{eval var=$line}

That did the trick for me.

-------------Problems Reply------------

You are probably looking for something like {eval}

Take a look at {eval} documentation.

On your situation, you could try this:

example.php

<?php
(...)
$var = array("{#OK#}", "{#CANCEL#}");
$smarty->assign('var', $var);
$smarty->display('example.tpl');
?>

example.config

OK = Okay
CANCEL = Nevermind

example.tpl

{config_load file='example.config'}

<h1>Template stuff</h1>

{if isset($var) }
{foreach from=$var item=line}
{eval var=$line}<br>
{/foreach}
{/if}

Hope that helps! :)

a great aproach I've seen was use modifiers for translations. this allow you to translate dynamic content.

all the code its just an example, wont work, just to give you an idea

lets say

your tpl

{"Hello word! How are you %s?"|translate:"Gabriel"}

{$myvar|translate:"Gabriel"}

your modifier

function smarty_modifier_translate($content, $args) {
$lang = Env::getLanguage();
return vsprintf($lang->getTranslation($content), $args);

}

As you probably noticed smarty parses your template into php code and stores it in templates_c directory. It makes this library run very fast. What you are going to accomplish would require to parse a completly new template every time a looped code is being executed. This would render your application very slow.

I would suggest not storing messages in constatnts, but to store it in templates, eg.

{assign var='lang' value='en'}
{if isset($var) }
{foreach from=$var item=line}
{include file="$lang/$line.tpl"}<br>
{/foreach}
{/if}

Category:php Views:1 Time:2009-05-25

Related post

  • Smarty Caching (With Dynamic Content) 2011-02-28

    I have a very dynamic (social networking) site running smarty that I want to enable caching for. My Structure: index.php display()s template.tpl template.tpl include()s indexContent.tpl Most of the content in template.tpl is static .. such as the scr

  • Block webpages which are only used for dynamic content retrieval through AJAX. (.htaccess?) 2012-03-13

    I would like to know if it's possible to make pages invisible for users with .htaccess or something else. Some background info and some other questions : I'm building a web application which does a lot of dynamic content loading. At the moment i'm ju

  • Embedded custom-tag in dynamic content (nested tag) not rendering 2008-09-08

    Embedded custom-tag in dynamic content (nested tag) not rendering. I have a page that pulls dynamic content from a javabean and passes the list of objects to a custom tag for processing into html. Within each object is a bunch of html to be output th

  • Options for Dynamic content in ASP.Net 2008-09-24

    What choices do I have for creating stateful dynamic content in an ASP.Net web site? Here's my scenario. I have a site that has multiple, nested content regions. The top level are actions tied to a functional area "Catalog", "Subscriptions", "Setting

  • Dynamic Content And Search Engine Optimization 2008-12-10

    I have a web forum that I have coded in Python for the App Engine platform. I have noticed that it is not being index well by Google and am trying to fix that. I have Google Webmaster to submit a sitemap with almost 400 URLs but apparently only 8 wer

  • IIS is closing connection on dynamic content 2009-02-18

    There is a strange problem I am facing, if any of you might have seen before. I have freshly install OS (Windows 2003 SP2) and just have configured IIS on it and on the default site,the static content say hello.htm is working fine.But any dynamic con

  • Auto Slider / Scroller to display dynamic content 2009-04-27

    Can any one tell me how can i implement a auto slider/scroller in my web page. The slider should show dynamic data from a database. (Ex : Hot jobs tab in the plipl.com site's home page (www.plipl.com) . Is there any easy way to do this with jQuery ?

  • Is it possible to zoom dynamic content with silverlight? 2009-05-07

    I am new one with Silverlight and I am interested in how to zoom dynamic content. For instance i have canvas and several listboxes and i want to zoom it. --------------Solutions------------- Yes. Every visual element within Silverlight has a number o

  • How do I disable chunked transfer-encoding when using compressed dynamic content? 2009-05-21

    I want to disable chunked transfer encoding on my web server, in order to avoid this bug: http://support.microsoft.com/kb/871205 . Unfortunately, I need to support IE6 on Win2k, so they cannot install the patch. I found instructions to set AspEnableC

  • Offline view of dynamic content? 2009-06-30

    I want to view dynamic contents (flash games, online transaction...etc) offline. For example, I finish level 1 of this cool flash RPG game. I go offline and play the level again. Or, I make a purchase. And make the purchase again offline. Of course t

  • Integrating dynamic content into mass email 2009-07-27

    We're looking for the best way to integrate dynamic content into emails sent by various individuals (or companies) using various mass mailing systems, some of them proprietary. What are the options to do that and what are the advantages and disadvant

  • Clear dynamic content with jQuery 2009-08-31

    After loading dynamic content to a DIV, I'd like to add a Close option, that would remove this content from the DOM. I'm working with jQuery and WordPress. Any ideas? --------------Solutions------------- Sure! You can use hide or remove based on your

  • Django dynamic content in VPS environment - when do I need a queue? 2009-09-20

    I've got some Django content that I plan to host on a vps, web facing. It dynamically generates images that are cached to disk (regeneration is not often needed except (1)user changes content within image or (2) the layout is updated globally so all

  • Adding ASP or Dynamic Content into the FCK textbox 2009-10-27

    I'd like to add some dynamic content into the FCK text box when I'm adding links, for example when I add a link or link some text I want to be able to put some ASP delimeters into the textbox and a variable name. Something like this <%= myLinkVari

  • How does gettext handle dynamic content? 2009-11-20

    In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content? I have 2 cases in mind. 1) Let's say I have <?=$user1?> poked John <?=$user2?>. Maybe in some language the order of the words is differ

  • Internationalizing dynamic content in Rails 2009-12-08

    We want to build a app where we would like to show translated version of dynamic content as well. The transalted content would be uploaded by us (so we don't need any translation logic). I was wondering if there any other gems to consider apart from

  • Generating dynamic content in Iframe IE 7 2009-12-23

    Is it possible to generate dynamic content inside Iframe? if yes , how ? I'm having some problems with IE, thank you UPDATE : I'm creating a modal window which plays video, but when I close it it remains playing in IE7 although its hidden but it fire

  • Dynamic content in 2 columns (rather than one!) 2009-12-24

    I have one table which display data as from Dynamic Content in 1 column. I would like the content to move to a second column when the number of cell is >3. (ie. if there are 3 cells to display, it would be display in 1 col, and 4 cells or more it

  • Capture generated dynamic content at server side 2009-12-26

    Is there any way with which I can capture generated dynamic content on the server side and get that file or string object of the same to the servlet. We can generate dynamic content with JSPs, but we dont have access to the generated dynamic content

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.095 (s). 11 q(s)