Jump to content

render() causes "Cannot redeclare" error


er314
 Share

Recommended Posts

Hello, 

In a template, I cycle through all site languages, 

and, for each language, 

 - I temporarily set $user->language to this language

 - I call $pages->get("/anotherpage/")->render()

in order to render this same content in all languages

Problem : 1st call to render() is ok, but on second call, error occurs : 

Compile Error:  Cannot redeclare functionxyz()

A few additional tests show that actually, render() kind of includes the called template code within the calling template code, and as a result, if any function name is already declared, this error occurs obviously.

Here, in my case, it's with the 2nd call to the called template that this occurs.

So, just to make sure, what's the best coding practice regarding this behavior ? Is there an API solution for avoiding the problem ?

Or should I adapt the code of the called template ? (by surrounding each function with "if(!function_exists" , or by putting all its functions in another file and include_once it)

thanks

Link to comment
Share on other sites

If you have only a few or one function in a template, you may wrap a if function_exists() around it.

If you have custom functions in other template files too, best way is to use a separate functions file. Depending on your setup you can include_once() your functions file in the site/config.php or a _init.php. This way you don't need to include it repeatedly in every single template file.

Link to comment
Share on other sites

thanks Horst, 


I'll go with cleaning the offending template, by removing the functions declarations, and include_once them. 


these functions are specific to the template, so I'll end up with thetemplate.php and include_once("thetemplate.inc")


  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...