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

Best way to get variable content into template
Goto page 1, 2  Next
 
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
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sat May 03, 2003 7:41 pm    Post subject: Best way to get variable content into template Reply with quote

Hi again Very Happy

I have started to implement Smarty with my site but I was wondering what the best way to handle getting the information into my template would be. Since my site is in two languages I want to pull things such as the title, a header for the page and the content each in the correct language. I have already addressed the language of the static elements on the page thanks to andre, boots and wom.bat (thanks again for your help guys Very Happy). However now I am faced with the choice of how to get this information into the template.

I could have each page send smarty the title, header and have it include a content file. However I was wondering if there would be a way to include all the information for one page into one file.

So one file could hold the following in both languages for easy modification:

Title_language1
Title_language2
Header_language1
Header_language2
Content_language1
Content_language2

I know such a method would be convenient for editing files as I could change the contents of both languages at the same time as well as the titles or header information if needed. But would such a method be feasible as well as efficient? If so could someone suggest how I might set this up using Smarty?
_________________
w00t Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sat May 03, 2003 10:39 pm    Post subject: Reply with quote

Good question. I take this as an application/data level problem and not a Smarty problem. There are many ways to solve this and choosing the right method has a lot to do with how your application is structured and how you wish to go about maintenance.

For example, the data can go into flat files, XML documents, databases, ini strings, or even PHP files--and others! Its hard to say which to pick without knowing how you've structured other portions of you application and data.

I'm a db guy. I like to put everything in dbs. Still, I typically use flat files and other mechanisms while I am working on/designing an app or until I feel that the data model is "correct". Then I will push everything to the db.

XML is an alright choice because it means your data will be "portable". However, you do get a parsing penalty and you will likely have to provide user tools to edit the contents since no one really likes editiing XML files by hand and it is a error ridden enterprise. If you have no direct need for XML or the features it provides, avoid it. My feeling is that a better data serialization format is on the horizon, anyhow Smile

Depending on the number of items, these values can even go into a Smarty config file, each language in its own section. Then you can do a config_load at the top of your templates, dynamically specifying which section to pick. This is actually cool because unlike the other solutions which have parsing / data access overheads, Smarty compiles config files meaning that the data is directly available to Smarty Smile As a result, you may want to keep your language data in seperate files so that only the data you actually need for a given language gets loaded (no need loading/reading data for all languages when only one language is required.). You may find that it is easier (and safer) editing two language files side-by-side as opposed to trying to jump around a single file. Separating them also reduces contention for a single resource.

That's the ten-mile view. See if your app suggests a particular pattern, talk to the people who have to do the actual editing about their working preferences and then post something more about your application details and I'm sure you'll get a more technical response than this one Smile

Good Luck!
Back to top
View user's profile Send private message
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sat May 03, 2003 11:36 pm    Post subject: Reply with quote

Thanks for the reply boots!

The site consists of about 25 or so pages, each page will have a version in English and one in French (I'm also in Canada btw Smile). The main issue is that we have to be able to edit the content with Dreamweaver. This being said I think I can luckily escape the possibility of XML Very Happy

With your approach are you suggesting that there be a different config file for each page, or one big one for each language? Also from what I can tell from the docs the format of a config file are
Code:

var1 = "something"
var2 = "something"
var3 = "something"

Considering the content is HTML formatted how would I get the content of the page in there?
_________________
w00t Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 12:15 am    Post subject: Reply with quote

A fellow Canuck! How nice!

As for your question, I suppose it depends on the amount of data you have, but my first reaction would be to use a single file for each language using sections to separate pages. You should consider how large this site will grow to, because I wouldn't use this technique if I was dealing with a lot of data.

ie.

site.conf.fr
[HOMEPAGE]
Title= ...

[ANOTHERPAGE]
Title= ...

----

site.conf.en
[HOMEPAGE]
Title= ...

[ANOTHERPAGE]
Title= ...


HTML in your language data? That doesn't sound right. Never-the-less, you can store your HTML data in config files.

This works!
Code:
[mysection]
var1 = <b>My Title</b>
var2 = <a href="http://www.phpinsider.com/smarty-forum/">Smarty Forum</a>


You may also want to use your data like:
Code:
{#TITLE#|default:"TITLE not set for $lang"}


To ensure that you can easily see where translators haven't finished their work. You can also do this via a custom function call or a custom modifier that loads a default value if the requested value is missing.

I hope it helps. I will let someone else post another techinique. Smile
Back to top
View user's profile Send private message
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sun May 04, 2003 12:23 am    Post subject: Reply with quote

Thanks again for the reply boots!

Maybe I am going about this all wrong, I was thinking of including the entire contents of the page (other than the template stuff of course). So in on a contact page instead of going line by line after each form field I would just include the whole HTML, form fields and HTML data included. I would imagine the HTML would confuse the config file the second it found an equals (=) sign.

Should I go about this another way?
_________________
w00t Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 12:49 am    Post subject: Reply with quote

Hi frost!

The config file probably will handle your HTML fine. It worked for me Smile.

I think I see what you mean about the includes, though I may have miscontrued your intentions. I do that sometimes, but always by accident. What I'm getting at is this:

Create your template (say contacts.tpl):
---
{config_load file=site.conf.$lang section=contacts}
<b>{#TITLE#}</b>
etc.

Now include it where you need it:
{include file="contacts.tpl"}

Is that not what you mean? See, the HTML formatting stays where it belongs--in the template--while your language file contains only the actual translation data.
Back to top
View user's profile Send private message
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sun May 04, 2003 12:58 am    Post subject: Reply with quote

I think I'm confused now Smile

What is in your contacts.tpl file?

What I want to do (or at least think I want to do) is have a contact page where someone can submit a form to contact us. So the page would be

Name: html_form_field
Email: html_form_field
Comments: html_form_field
Submit

Instead of translating all the lables seperatly (eg Name, Email, Comments) I would instead include the entire HTML code, the labels and the "html_form_field's".

Despite the fact that I'm trying to do this, maybe it isn't the best way?
_________________
w00t Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 1:05 am    Post subject: Reply with quote

Quote:
Instead of translating all the lables seperatly (eg Name, Email, Comments) I would instead include the entire HTML code, the labels and the "html_form_field's".


Personally, I think its a bad idea. If you change the styling or layout of the form, you have to make the corresponding change in each page for each language.

My suggestion, once again, is to template the language elements. Your example would look something like this:

{config_load file=site.conf.$lang section=contacts}
{#Name#}: html_form_field
{#Email#}: html_form_field
{#Comments#}: html_form_field
{#Submit#}

You don't have different templates for each language--but you DO have a different config file for each language, like config.site.en and config.site.fr. All of the formatting rules stay in the templates. All of the language data (and only the language data) stays in the config files.

Is that clearer?
Back to top
View user's profile Send private message
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sun May 04, 2003 1:09 am    Post subject: Reply with quote

Yes, yes it does thnx! I'm sorry I didn't quite follow you the first time Smile

This seems like a much better solution.

Thank you for all your help!
_________________
w00t Smile
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 1:15 am    Post subject: Reply with quote

I'm glad I can help! I still hope others post their solutions to this problem, though!
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sun May 04, 2003 9:56 am    Post subject: Reply with quote

I just want to present my solution once again, I know most people use constants defined in an external file and then set the "correct" things in their templates, which does, unfortunately, consume slightly more time.
I personally always do things like
Code:
{if ($_SESSION.language == "EN")}Elvis has left the building{elseif ($_SESSION.language == "DE")}Elvis hat das Gebäude verlassen{elseif ($_SESSION.language == "FR")}Elvis a quitté le bâtiment{/if}

sorry for my bad french Smile
you might also slightly modifiy this way so that you needn't translate whole pages in one rush:
Code:
{if ($_SESSION.language == "DE")}Elvis hat das Gebäude verlassen{else}Elvis has left the building{/if}

here, you haven't got a french translation yet, so the visitor sees english text (I don't use this method, as I don't like it much *g*)
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Sun May 04, 2003 10:05 am    Post subject: Reply with quote

Wom.bat, out of curiousity, why do you say your method is faster? Once the config files are compiled they look like this:

Code:
<?php $_config_vars = array (
  'callme' => 'Call me boots, please!<br/>'
); return true; ?>


So that when they are included, the required values are directly inserted into the local namespace. Lookups of ##'s are fast, especially since less variable decoding is required than for $. Also, it requires no in-template logic (which increases both compile time and execution time). Finally, the compiler has far less text to scan and process when using config files.

Finally, template updates don't affect the compilation of the language files.

I think your solution has merit--I'm not convinced that it is faster Wink

Can you please post the link to your related thread--I think it will be useful to some.

ps. You may recall that I'm not big on config files, but I do think they can be fast!
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sun May 04, 2003 10:27 am    Post subject: Reply with quote

err... sorry, I didn't mean "faster" in terms of execution time, but the time someone needs to build templates Smile

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=84
Back to top
View user's profile Send private message
frost
Smarty Rookie


Joined: 29 Apr 2003
Posts: 13
Location: Canada eh?

PostPosted: Sun May 04, 2003 2:09 pm    Post subject: Reply with quote

Thank you for your reply Wom.bat, I am however curious as to how you get the acctual content of the page (body/text) into your templates? Very Happy
_________________
w00t Smile
Back to top
View user's profile Send private message
Wom.bat
Smarty Pro


Joined: 24 Apr 2003
Posts: 107
Location: Munich, Germany

PostPosted: Sun May 04, 2003 2:22 pm    Post subject: Reply with quote

I usually determine the correct language version in my PHP code and then just assign the right version to the template Smile
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
Goto page 1, 2  Next
Page 1 of 2

 
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