How to check variable isset using template variables?

851 views
Skip to first unread message

Andrew Quan

unread,
Feb 20, 2013, 4:19:37 AM2/20/13
to f3-fra...@googlegroups.com
I would like to do something like the following:

{{ if (isset(@title)) @title }}

Andrew Quan

unread,
Feb 20, 2013, 4:55:39 AM2/20/13
to f3-fra...@googlegroups.com
Currently doing this:

<check if= "isset(@title)"><true>{{ @title }}</true></check>

xfra35

unread,
Feb 20, 2013, 5:07:09 AM2/20/13
to f3-fra...@googlegroups.com
{{isset(@title)?@title:''}}

Andrew Quan

unread,
Feb 20, 2013, 5:11:40 AM2/20/13
to f3-fra...@googlegroups.com
Shweet, thanks xfra35 :)

solo

unread,
Mar 19, 2017, 12:37:48 PM3/19/17
to f3-fra...@googlegroups.com
Bringing up an old thread, but I have found that in some cases, using this exact code, even when "@title" is set to something, it evaluates as false.  What could cause this in F3?

Code sample:
{{@title.name}}<br/>
{{isset(@title.name)?'YEP':'NOPE'}}<br/>

This outputs:
Johnson
NOPE



Odd right?

ved

unread,
Mar 20, 2017, 8:14:02 AM3/20/17
to Fat-Free Framework
Odd yes, but that doesn't happen on any of my several F3 installs.

What version are you using?

solo

unread,
Mar 20, 2017, 8:20:23 AM3/20/17
to Fat-Free Framework
If I go by the changelog in /lib:
3.6.0 (19 November 2016)

My composer calls:
"bcosca/fatfree": "dev-master",

I haven't run a composer update recently either, if that matters.

I ended up simply changing my template to
{{@title.name}}

...instead of using isset in this similar example on some of my pages that weren't working.  On other pages in the same project, it seems to still work OK... which is extremely odd... if there are any suggestions to try / output let me know and I will copy/paste results here for you.

ved

unread,
Mar 20, 2017, 8:31:25 AM3/20/17
to Fat-Free Framework
Wait, but you have F3 inside a "lib" folder and also composer at the same time?

Wouldn't composer save f3 inside the vendor folder and make the lib folder unnecessary?

How are you initializing F3?

$f3 = require 'base.php';

or

$f3 = \Base::instance();

?

solo

unread,
Mar 24, 2017, 5:27:02 PM3/24/17
to Fat-Free Framework
Sorry for the delay!

Composer is installing per its default in a vendor folder.  I just went to the /lib folder to find the changelog, I wasn't sure where else to get the "latest version" information...

vendor\bcosca\fatfree\lib


I initialize F3 on index.php as follows (and again in Controller.php, hopefully this is correct use!):

 require_once __DIR__ . '\vendor\autoload.php';
$f3
= Base::instance();
$f3
->config('app/config/config.ini');
$f3
->config('app/config/routes.ini');
$f3
->run();


Then, in my Controller.php (this is the parent controller to everything else) it looks like this:
class Controller {
 
 
//Main Variables include DB
 
protected $f3;
 
protected $db;


 
function __construct() {

 $f3
=Base::instance();
 $this
->f3=$f3;

    $db
=new DB\SQL(
        $f3
->get('dbdmd'),
        $f3
->get('dbusername'),
        $f3
->get('dbpassword'),
        array
( \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION )
   
);

    $this
->db=$db;
 
}
}

solo

unread,
Apr 8, 2017, 7:47:53 AM4/8/17
to Fat-Free Framework
Still experiencing this, driving me nuts =).  Any debugging tips?

ved

unread,
Apr 8, 2017, 10:17:07 AM4/8/17
to f3-fra...@googlegroups.com
What happens if you use:

{{!empty(@title.name)?'YEP':'NOPE'}}

instead of

{{isset(@title.name)?'YEP':'NOPE'}}

?

EDIT: Also, what does that "in some cases" mean? Can you specify on which cases does that happen?

tom chris

unread,
Apr 10, 2017, 4:41:56 AM4/10/17
to Fat-Free Framework
Try {{ (@title.name)?'YEP':'NOPE'}} it will work

solo

unread,
Apr 10, 2017, 7:14:56 PM4/10/17
to Fat-Free Framework
This code:
1. {{!empty(@data.name)?'YEP':'NOPE'}}<br>
2. {{isset(@data.name)?'YEP':'NOPE'}}<br>
3. {{ (@data.name)?'YEP':'NOPE'}}<br>


Outputs:
1. NOPE
2. NOPE
3. YEP

VERY odd.

This page route is: 
domain.com/route1/@id/update


which loads values from the DB and then sends them to the template through:
$this->f3->set('data', $data);
$this->f3->set('view', 'mypage.htm'); // Set the main view
        $template=new Template;
        echo $template->render('layout.htm'); // Call main layout

My other pages use the same code to send data to the template, however they are loading data from POST instead of the database is all.  I don't see why these use cases would be any different to isset and empty ?

xfra35

unread,
Apr 11, 2017, 3:45:41 AM4/11/17
to Fat-Free Framework
What's the value and type of @data.name?

Do you experience the same issue outside of the template?

ved

unread,
Apr 11, 2017, 4:24:21 AM4/11/17
to f3-fra...@googlegroups.com
Well, according to his previous example, @title.name was just a string.
He also stated that this happens "in some cases" but didn't specify exactly which cases.
I'm guessing the issue is related with those cases.

Can you try this on your template:

<pre>{{ var_dump(@data) }}</pre>

And paste the output here?


On Sunday, March 19, 2017 at 4:37:48 PM UTC, solo wrote:

solo

unread,
Apr 11, 2017, 6:38:16 AM4/11/17
to Fat-Free Framework
I was noting previously that the difference is I am grabbing from a DB here, vs. just displaying POST data in other cases.
My other pages use the same code to send data to the template, however they are loading data from POST instead of the database is all.  I don't see why these use cases would be any different to isset and empty ?

With vardump, I see now that this is an F3 object since it was retrieved from the DB via F3...  The object has my database info in it along with a lot of other data from the database - so I prefer not to paste it here =).  Can I not appropriately use isset() on an object in this manor?

Here is the output related to data.name, however:

 ["fields":protected]=>
  array(17) {
    ["name"]=>
    array(8) {
      ["type"]=>
      string(12) "varchar(255)"
      ["pdo_type"]=>
      int(2)
      ["default"]=>
      NULL
      ["nullable"]=>
      bool(false)
      ["pkey"]=>
      bool(false)
      ["value"]=>
      string(20) "John (Johnny)"
      ["initial"]=>
      string(20) "John (Johnny)"
["changed"]=> bool(false) }

Thanks.

ved

unread,
Apr 11, 2017, 7:17:36 AM4/11/17
to Fat-Free Framework
To get the results from the mappers in an array form instead of an object, you can use $mapper->cast() as described here.

solo

unread,
Apr 11, 2017, 7:30:50 AM4/11/17
to Fat-Free Framework
Makes sense, thanks.

What would be the best practice for this situation, always cast to an array first so you don't send the full object to the template?

ved

unread,
Apr 11, 2017, 7:40:48 AM4/11/17
to Fat-Free Framework
No problem.

I personally usually use cast() when passing stuff directly from the database to the template since I prefer to have all template variables use @array.value format instead of @obj->value.

I guess it's mostly a personal preference thing but as you've stated, passing the array can probably be a bit lighter in terms of data moving around but I'm not sure if there are any noticeable differences.
Reply all
Reply to author
Forward
0 new messages