Welcome Guest, Not a member yet? Register   Sign In
Undefined pseudo-variables
#1

[eluser]olimortimer[/eluser]
Quick question...

If you don't define a pseudo-variable, it shows as normal text when the page renders. Is there any way of setting undefined pseudo-variables to false, without manually defining them in a controller, whilst the template engine parsers defined ones correctly?
#2

[eluser]InsiteFX[/eluser]
This should point you in the right direction.
Code:
<?php echo isset($content) ? $content : ''; ?>
#3

[eluser]olimortimer[/eluser]
Thanks, but that means I have to do that manually in each controller.

For example, if I'm using the same template for all pages, including pages with contact forms on, I want to have {feedback} for form feedback. Now instead of defining $data->feedback = false; in all controllers that use the same template, I'd like to extend the template / parser to set any undefined pseudo-variables as false.

I'm guessing this isn't easy though, as the template / parser would need to know what variables are waiting in the template file.
#4

[eluser]Kamarg[/eluser]
Why do it in each controller? Why not just do what InsiteFX said in the template itself?
#5

[eluser]vbsaltydog[/eluser]
@kamarg

I think oli is using a templete engine that uses {variable-name} in the view files. He wants {variable-name} to parse nothing if the var doesn't exist.

@oli

Why not use the MY_Controller class to set defaults for your common variables and override them at the controller level if needed?

Code:
class MY_Controller
{

public function __construct()
{

parent::__construct();

$common_vars = array('var1' => '',
'var2' => '',
'var3' => '',
'var4' => ''
);

$this->load->vars($common_vars);

}//End of function

}//End of class
#6

[eluser]Aken[/eluser]
If you have global variables that you want available in all your views, maybe creating your own custom Base Controller would be a better route?
#7

[eluser]vbsaltydog[/eluser]
[quote author="olimortimer" date="1327952541"]Quick question...

If you don't define a pseudo-variable, it shows as normal text when the page renders. Is there any way of setting undefined pseudo-variables to false, without manually defining them in a controller, whilst the template engine parsers defined ones correctly?[/quote]

I think you are confusing pseudo-variables with class variables.

$this is a pseudo-variable.
$this->var_name is a class variable.
#8

[eluser]InsiteFX[/eluser]
You still need to do it in the controller, I played with this tonight and found this out.
Code:
public function index()
{
    $this->load->library('parser');

    /**
     * either way you still need to secify the data or it will
     * display the pseudo-variable! You need to include the html tags
     * in the data or they will display and mess up your layout!
     */
    $data = array(
        //'blog_title' => '<h1>My Blog Title</h1>', // this will display
        'blog_title' => '',    // this will make it blank if it does not exist
        'blog_heading' => '<h3>My Blog Heading</h3>'
    );

    $this->parser->parse('welcome_message', $data);
}

So if your not using the vaiable you still need to define it!
#9

[eluser]CroNiX[/eluser]
It sounds like you should be creating different templates that are specific to the data you are sending it instead of an all purpose template that may or may not have certain pieces of data sent to it. You just need to rethink the approach. Also, if you use the parser you are limiting your options in this way.

You could, of course, extend the parser class to output an empty string in those cases.
#10

[eluser]olimortimer[/eluser]
Cheers guys.

Lab Assistant was correct, in that I'm using the built in Template engine that uses {variable-name}, which are called pseudo-variables in the user guide, in the views files.

I could create different templates for the "special pages", but I'm trying to keep it to as little view files as possible, so it allows future changes to the core HTML to be as easy as possible.

Like Sr. Research Associate mentioned, if you don't define a variable, the template engine just leaves the {variable-name} just like that, which obviously outputs to the screen. To output blank, you either need to define it as '' or as false.

The use of MY_Controller, as Lab Assistant mentioned, sounds like the perfect solution, so just about to give it a go.




Theme © iAndrew 2016 - Forum software by © MyBB