Can we use {variable} in Codeigniter Views?

Asked

Viewed 591 times

4

I know we can use Views.html file in Codeigniter, now I wonder if we can use tags {variavel} directly as in Smarty.

Example:

<body>
  <h1>{msg}</h1>
</body>

Are there any native replacement functions? Or do you have to use one more docked framework?

I would like to completely separate PHP from HTML, today I already do it with a class of my own, but I wonder if Codeigniter already does this.

As an example in substitution:

$data = array();
$CI = & get_instance();
$page = $CI->load->view('login.html', $data, true);
echo   str_replace("{teste}", "Hello World", $page);
  • 1

    Codeigniter does not come with any template engine. Change <?php echo $msg; ?> for {$msg} natively is not possible!

  • I work with the IC, but as far as I know, it is necessary to adapt so that it recognizes these variables. I have never tried to use, always use $var->field;

  • Only the example title see the lines: $data = array(); $CI = & get_instance(); $page = $CI->load->view('login.html', $data, true); echo str_replace("{test}", "Hello World", $page);

  • 1

    Edit the question and add this information. You haven’t commented on anything before replacements. I don’t think anything practical about these str_replace() ...

  • In fact str_replace is only for demonstration purposes, it should have a more user-friendly medium

  • I’m sorry guys it’s my first post on the stack I’ll get the editor’s way to format better.

  • 1

    You can edit the question code with the button { } or identifying each line of code with 4 spaces :)

  • @rray think it’s the parser, as it was answered, huh

Show 4 more comments

1 answer

2

The codeigniter has a parser library for that it needs, take a look http://www.codeigniter.com/user_guide/libraries/parser.html

And for the purpose of leaving here as it would be is something like this:

// carrega a library
$this->load->library('parser');

// le/gera os dados
$data = array(
            'blog_title' => 'My Blog Title',
            'blog_heading' => 'My Blog Heading'
        );

// carrega a view blog_template e passa os dados
$this->parser->parse('blog_template', $data);

and the view would look something like:

<html>
    <head>
        <title>{blog_title}</title>
    </head>
    <body>

        <h3>{blog_heading}</h3>

    </body>
</html>
  • Is that lib only available in CI version 3? + 1 for the tbm answer

  • no, version 2 already had it and I don’t remember if version 1.* had it either.

  • I managed to find the link from the CI2 documentation, it’s the first time I’ve seen anyone comment on that lib.

  • very likely because whoever starts using realizes that it is slower to render everything in the end and is very basic, but depending on what it breaks a branch.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.