TinyButStrong - the PHP Template Engine
Categories > Your tips & tricks >

link translation for images in template

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: kle_py
Date: 2011-05-25
Time: 21:58

link translation for images in template

I would like to share this piece of code with You. Maybe it is useful to someone.
It seems like the following way i can resolve a problem, which complicated my site-design for a long time. Now i can design my templates with the correctly from within the modules section, and still have it working, although i include the template in another master template. My solution: link-translation ;) .

What do i mean ?
I have the following files:

inside /public/mastertemplate.htm i include the local template of the module, like:
<div id="content-body">

Now i design the local template /modules/module1/template.htm using the original images in /public/images/images/ using relative links.
for example:
<div style="background-image: url(../../public/images/logo40green.jpg); background-position: right top; background-repeat: no-repeat;">
<h2>Title for detail: #[var.id]</h2>

However this will NOT work like this in the final incuded page !
My trick is to dynamically re-write parts of the image link directly after loading the template - before showing it:
// i am within the file /public/index.php
$TBS->Source = str_replace('../../public', '../public', $TBS->Source);

The same could be applied to links to css and javascript. It lets me design within the module area, using the original "public" static elements, and still keeps working, although the public page is called from another directory level.

Maybe there is a more elegant way, but done as shown above is usefull for me right now ;)
By: Skrol29
Date: 2011-05-26
Time: 00:47

Re: link translation for images in template

Thanks for your tip.
I must say I used a similar way for managing this TBS site that may display some pages in both paths "/" and "/fr/".