View previous topic :: View next topic |
Author |
Message |
fortuity Smarty Rookie
Joined: 23 Dec 2003 Posts: 20 Location: Marin County, California
|
Posted: Thu Apr 01, 2004 6:28 pm Post subject: template collision for pages with same name |
|
|
My site organization requires template files in the same directories as php files. There are multiple directories and some pages have the same names.
/news :
-----------
/news/index.php
/news/index.tpl
/about :
-----------
/about/index.php
/about/index.tpl
Each index.php file includes the same prepend.php file which instantiates Smarty and sets a single shared template compile directory:
/includes/prepend.php:
$smarty = new Smarty();
$smarty->compile_dir = '/var/www.sitename.com/compile/';
An index.php file looks like this:
define('APP_PATH', dirname(dirname(__FILE__)) . '/includes/');
require_once(APP_PATH.'prepend.php');
$smarty->assign('something', 'something');
$smarty->display('index.tpl.html');
Experienced Smarty users will immediately recognize that my compiled templates are colliding because they share the same names in a shared compile directory. If I visit /news/index.php and then /about/index.php I only ever see the page generated for /news/index.php.
Will my problem be resolved if I set
$smarty->use_sub_dirs=true ?
Or is there another way I can force Smarty to generate compiled templates in separate subdirectories? I'd like Smarty to know that templates with the same name are in different directories and create compiled template files in corresponding subdirectories. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu Apr 01, 2004 7:35 pm Post subject: |
|
|
hmm. Why do I think that this is only true if the path/name collide (not just the name)?? That would only occur if you are using multiple template_dirs to the same path or are using some sort of template_dir array. In either case, you need to have more compile dirs. |
|
Back to top |
|
fortuity Smarty Rookie
Joined: 23 Dec 2003 Posts: 20 Location: Marin County, California
|
Posted: Fri Apr 02, 2004 7:40 am Post subject: |
|
|
I asked,
> Will my problem be resolved if I set
> $smarty->use_sub_dirs=true ?
I experimented and "use_sub_dirs=true" makes no difference in this situation (safe mode is off). I still get template collisions. The collisions go away if I set "$smarty->force_compile=true" but that's not what I want for deployment.
boots said,
> That would only occur if you are using multiple template_dirs to the same path
Yes, I am. Each directory (about/, admin/, news/) contains templates and the prepend file specifies one common compile directory.
> you need to have more compile dirs
Apparently Smarty's design requires this. For each directory that contains templates I have to explicitly specify (and create and set writable) a corresponding template_compile directory. I was hoping to only have to specify/mkdir/chmod a single compile directory and have Smarty do the work of creating subdirectories to match each template directory as needed. Unless someone knows otherwise it appears this can't be done.
Just to explain my site's design requirements, the idea of one big template directory is undesirable. The site is segregated with different directories to match its information architecture and page designers need to find the template file that builds a page in the directory that matches the URL seen in the browser (adjacent to the php file for the page).
It seems it's best if I let Smarty use the default compile_dir (it will look for a "templates_c" subdirectory in any directory that contains a template).
However, the Smarty "multiple compile directories" requirement makes it complicated for anyone to download and install the site because I have to ask them to search for all the "templates_c" subdirectories and chown or chmod each one. I was hoping they could just find one compile directory and chmod that.
Anyone know of a way to use multiple template_dirs and a single compile directory? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Fri Apr 02, 2004 7:49 am Post subject: |
|
|
fortuity wrote: | I asked,
> Will my problem be resolved if I set
> $smarty->use_sub_dirs=true ?
|
no, it won't. use_sub_dirs is something completely different.
what you want is: set your $smarty->template_dir to "/var/www.sitename.com"
(or where your tpl- and php-files are).
and display them like
$smarty->dispaly('news/index.tpl');
$smarty->dispaly('about/index.tpl');
that will make smarty see the two a two distinct templates. and not as one "index.tpl".
HTH
messju |
|
Back to top |
|
fortuity Smarty Rookie
Joined: 23 Dec 2003 Posts: 20 Location: Marin County, California
|
Posted: Fri Apr 02, 2004 8:09 am Post subject: |
|
|
messju wrote: |
set your $smarty->template_dir to "/var/www.sitename.com"
and display them like
$smarty->dispaly('news/index.tpl');
$smarty->dispaly('about/index.tpl');
|
That doesn't work because the file
/news/index.php
is adjacent (same level) with
/news/index.tpl
and Smarty looks for (and can't find)
/news/news/index.tpl
My requirement is that the .tpl and .php file are side-by-side, not in separate directories.
Incidentally, I tried using the cache_id technique, like:
$smarty->display('index.tpl.html',"admin");
but I continued to have template collisions. I guess cache_id only affects caching, not template compiling.
Maybe I can resolve this if I specify an absolute path like
$smarty->display(PATH.'/index.tpl') ? |
|
Back to top |
|
fortuity Smarty Rookie
Joined: 23 Dec 2003 Posts: 20 Location: Marin County, California
|
Posted: Fri Apr 02, 2004 8:23 am Post subject: |
|
|
fortuity wrote: | Maybe I can resolve this if I specify an absolute path like
$smarty->display(PATH.'/index.tpl') ? |
I just tried
$smarty->display(dirname(__FILE__).'/index.tpl');
and it appears to resolve the template collision issue. It accommodates multiple template files and a single compile directory.
Any comments why this might not be a good idea?
I'm surprised no one else mixes their templates into the same directories as their php files. Why is that an uncommon practice? I've seen it with other templating systems (WebObjects comes to mind). My php files are very simple (just presentation layer functionality, instantiating objects that do the heavy lifting elsewhere). But it seems it is not a Smarty "best practice." |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Apr 02, 2004 10:56 pm Post subject: |
|
|
If you have a module approach like you are showing, then it makes sense to mix. The consequence is that your code files are more tightly bound to your template files and vice-versa. If you have any sort of generalized templates (for example, which can match more than one code file) or if your app/design teams or more segregated, then not mixing makes sense to me.
Your solution is simple and clever. FYI, it should be possible for you to write a custom resource and set the default resource type to it to achieve the same thing transparently (ie. delegate the path location task to the resource). |
|
Back to top |
|
fortuity Smarty Rookie
Joined: 23 Dec 2003 Posts: 20 Location: Marin County, California
|
Posted: Mon Apr 05, 2004 6:08 am Post subject: |
|
|
Thanks for the help and the thoughtful comments. I'm happy Smarty imposes no inherent limitations on this approach. |
|
Back to top |
|
stanhecht Smarty n00b
Joined: 31 Jul 2004 Posts: 1 Location: Los Angeles
|
Posted: Tue Aug 03, 2004 8:16 pm Post subject: |
|
|
Big thank you for this solution. I'm developing an intranet site with a lot of pages in a directory tree. I was trying to figure out how to put the php files and related template files in the same directory. Tried a few things, with no luck, until I found this post.
My assorted common templates (header, footer, etc) stay in a single templates directory:
define('SMARTY_TEMPLATE_DIR', ROOT_DIR.'templates');
$smarty->template_dir = SMARTY_TEMPLATE_DIR;
As for the main page templates, I put them in the same directory as their php pages and then display them using dirname(__FILE__), as suggested earlier in this thread.
So far, seems to work well. Thanks again. |
|
Back to top |
|
|