Welcome Guest, Not a member yet? Register   Sign In
Views/Rendering Variable Issues?
#1

(This post was last modified: 10-11-2016, 11:22 AM by enlivenapp.)

I have a project that will be built on CI4 and I have some time to play around with it today for giggles.

I'm loosely following the Static Pages Tut and have noticed there's issues (for me) passing variables through view calls.  I have a pretty good idea why this is happening and wondering if there's a workaround other than passing each expected variable through the view call.  so....


I have a directory that looks like this:
Code:
...
- Views
 - Templates
     - Layouts
        - default.php
     - Partials
        - Default
            - meta.php
            - header.php
            - footer.php
        - etc...


in my controller, I do the usual stuff, nothing special, of calling views...
Code:
public function index()
    {
        $data['title'] = "Test Page";

        
        echo view('templates/layouts/default', $data);
    }

All goes well so far, here's where it get's a bit troublesome.


In Templates/Layouts/Default/default.php
Code:
<!doctype html>
<html>
<!-- call meta partial -->
    <?php echo view('templates/partials/default/meta') ?> // throws missing variable error

       <?php echo view('templates/partials/default/meta', ['title' => $title]) ?>  // usable variable in partial view
<body>

       <h1><?= $title; ?></h1>

in Templates/Partials/Default/meta.php
Code:
<head>
       <title><?= $title; ?></title>
</head>

One would expect the $title variable to be available as it's being called from the layout file, which has access to the $data['title'] variable but CI throws an error about the missing variable $title.

I'm still getting read up on the docs, and there's not much currently available about templates/rendering/etc, so I'm relying on CI2/3 conventions for this experiment.  I have read a bit about view cells, should I be using that or is CI4 just not quite up to speed on the views/rendering/etc stuff?

Just to make sure I'm being fairly clear about it all, I was expecting access to all the $data passed from the controller to the view in all the Layout and Partial views.
Reply
#2

We opted to be a little more secure in the handling of data this time around and to NOT save data between different view() calls by default, but require it to be explicitly passed. This is a change from previous versions. You should be able to pass [b]saveData[\b] through with a value of [b]true[\b] in the options array to have it work like it did previously, though.

Code:
echo view('templates/layouts/default', $data, ['saveData' => true]);
Reply
#3

(10-11-2016, 12:44 PM)kilishan Wrote: We opted to be a little more secure in the handling of data this time around and to NOT save data between different view() calls by default, but require it to be explicitly passed. This is a change from previous versions. You should be able to pass [b]saveData[\b] through with a value of [b]true[\b] in the options array to have it work like it did previously, though.

Code:
echo view('templates/layouts/default', $data, ['saveData' => true]);

Thanks for your reply.   Adding saveData into the options array does the trick...

not sure I want to always override default behavior though...  I may ponder on it a while...   I may end up writing/using a template engine yet...

Thanks again!
Reply
#4

(10-11-2016, 01:00 PM)enlivenapp Wrote: Thanks for your reply.   Adding saveData into the options array does the trick...

not sure I want to always override default behavior though...  I may ponder on it a while...   I may end up writing/using a template engine yet...

Thanks again!

Since this goes against the way people are used to using it, this might be a good prospect for a config setting, too...
Reply
#5

Its too early for project build with CI4..
Best VPS Hosting : Digital Ocean
Reply
#6

(10-11-2016, 09:35 PM)kilishan Wrote:
(10-11-2016, 01:00 PM)enlivenapp Wrote: Thanks for your reply.   Adding saveData into the options array does the trick...

not sure I want to always override default behavior though...  I may ponder on it a while...   I may end up writing/using a template engine yet...

Thanks again!

Since this goes against the way people are used to using it, this might be a good prospect for a config setting, too...

Possibly, but more secure data handling is usually a good idea.   Smile

If there's a way to handle/pass the data to multiple views in a singe view/template call more securely, it might be best to prescribe that way... if not, someone would need to know (docs?) the need to pass saveData = true when building their template engine or calling view().

@sv3tli0

Quote:...some time to play around with it today for giggles.
Reply
#7

(10-11-2016, 09:35 PM)kilishan Wrote: Since this goes against the way people are used to using it, this might be a good prospect for a config setting, too...

I was "lectured" on this forum and in PM about this issue. I was urging that it stay the same. But since then my attitude has changed. Its just templates - not every view file - and its very quick to add those few lines of code to some templates. I think every decision about CI 4 should be based on: "is this the best way to build a modern PHP 7 framework?"
Reply
#8

(10-12-2016, 12:55 PM)cartalot Wrote:
(10-11-2016, 09:35 PM)kilishan Wrote: Since this goes against the way people are used to using it, this might be a good prospect for a config setting, too...

I was "lectured" on this forum and in PM about this issue. I was urging that it stay the same. But since then my attitude has changed. Its just templates - not every view file - and its very quick to add those few lines of code to some templates. I think every decision about CI 4 should be based on: "is this the best way to build a modern PHP 7 framework?"

It's already been added. https://github.com/bcit-ci/CodeIgniter4/...4c9c4e3f5f

I agree though,  in a template engine, it would be trivial to set saveData to true every time the template engine is called, but I do understand (and agree) with why the default behavior was changed (data leaking, etc), but it's a problem for templates with multiple view() calls. 

I don't know this is a "best way to build a modern PHP 7 framework" decision though, it more comes down to best practices and security interpretation IMO.  Regardless of how this issue settles out, the core team is giving us the options we need to use it and it's appreciated.
Reply
#9

(10-12-2016, 01:11 PM)enlivenapp Wrote: It's already been added. https://github.com/bcit-ci/CodeIgniter4/...4c9c4e3f5f

wow thats so awesome - thanks for posting this - and Thanks Lonnie!!
Big Grin
Reply




Theme © iAndrew 2016 - Forum software by © MyBB