1
fatman
smarty and using full path includes
  • 2004/2/4 16:00

  • fatman

  • Friend of XOOPS

  • Posts: 176

  • Since: 2003/12/13


Hi, I'm trying to debug an issue with a module that is in beta. My module uses mod_rewrite to provide clean(er) urls. Because of this every time I include a file I use the full path.

example
// I always use this
include_once(XOOPS_ROOT_PATH.'/modules/include/mycommon.php');

// I don't use this 
include_once('include/mycommon.php');


The problem comes in when I want to pass along the full path to a file which will then be included at the template stage.

probably best to use another example
// In my module code I want to change the template

if ( $cart_items )
{
  
$my_template 'cart_contents.html';
}
 else
{
  
$my_template 'featured_products.html';
}

// Now I assign this to a smarty variable

$xoopsTpl->assign('my_template',
XOOPS_ROOT_PATH."/modules/noah/templates/pages/".$my_template); 

---------------------------------
// Then in my smarty template
---------------------------------

<{include 
file=$my_template}>


This works fine when running on unix/apache and seems to cause problems when running on windows/apache. I think this might be because windows and unix path strings are formatted differently, but I'm not smart enough to confirm this :)

hmmm, is this perhaps an issue of better code organization? As in, should I be making sure that everything happens before..

include(XOOPS_ROOT_PATH.'/header.php');

.. so that I can then dynamically assign

$xoopsOption['template_main'] = 'main.html';

?? anyone have suggestions?

Forum thread with some more background

2
Mithrandir
Re: smarty and using full path includes

This is a module for XOOPS?

If so, you should use <{include db:FILENAME}> as SMARTY templates are saved in the database in XOOPS.

However, I'd say that re-designing the module pages to set the template_main as early as possible, then the header.php and finally the data retrieval code is a better way to avoid too much logic inside the SMARTY templates.

3
fatman
Re: smarty and using full path includes
  • 2004/2/4 16:28

  • fatman

  • Friend of XOOPS

  • Posts: 176

  • Since: 2003/12/13


yes, the module is for XOOPS and can be demo'd here noah.kerkness.ca

I did not know about <{include db:FILENAME}> and will give that a try. thanks.


4
fatman
Re: smarty and using full path includes
  • 2004/2/4 17:29

  • fatman

  • Friend of XOOPS

  • Posts: 176

  • Since: 2003/12/13


can you provide an example of how to use <{include db:FILENAME}> correctly?

I've tried the following in my module and it is producing fatal smarty class error's on update.

[code]
// in my xoops_version.php
$modversion['templates'][1]['file'] = 'shop.html';
$modversion['templates'][1]['description'] = 'base'; $modversion['templates'][2]['file'] = 'template_name.html';
$modversion['templates'][2]['description'] = 'included template';


// in my module code
$xoopsOption['template_main'] = 'shop.html';
include(XOOPS_ROOT_PATH.'/header.php');
$xoopsTpl->assign('my_template', 'template_name.html');
include(XOOPS_ROOT_PATH.'/footer.php');

// in my smarty template file 'shop.html'
<{include db:$my_template}>

// this produces the following error when
// I am updating the module, so all the template
// details load into the db.

Fatal error: Smarty: [in db:shop.html line 1]: syntax error: invalid attribute name: 'db:$my_template'

[code]

I'm most likely structuring this the wrong way. Any suggestions?

5
Mithrandir
Re: smarty and using full path includes

<{include file='db:filename.html'}>

I'm not sure whether you can do dynamic includes, but try it out
<{include file='db:$my_template'}>

6
fatman
Re: smarty dynamic included solved
  • 2004/2/4 18:19

  • fatman

  • Friend of XOOPS

  • Posts: 176

  • Since: 2003/12/13


you can do a dynamic include if you use double quotes and not single quotes in your smarty syntax

example

// double quotes work because the when the parser
// sees double quotes it knows to parse the enclosed
// strign for variables.

<{include file="db:$my_template"}>


// When you just use single quotes the parser
// does not bother to look for variables, thus
// saving itself a little effort.
// the following doesn't work
<{include file='db:$my_template'}>



big props to Mithrandir for pointing me in the right direction.

7
Mithrandir
Re: smarty dynamic included solved

Thread bookmarked
Could be useful to remember this.

Login

Who's Online

378 user(s) are online (284 user(s) are browsing Support Forums)


Members: 0


Guests: 378


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits