Jump to content

$field->renderValue


Can
 Share

Recommended Posts

Hey friends :-)

Really love the new $field->render and $field->renderValue, I'm using it more and more, the code gets shorter, more modular and therefore less duplicates. :D

Thank you Ryan for this great addition.

Checked Inputfield.php and the two blog posts about the new methods but couldn't really find what I'm looking for.

Is it already possible to pass in custom variables to the field template?

Like when using TemplateFile and delegate output you can simply do $templateFile->foo = $foo;

Maybe something like $field->renderValue($value, '/fieldtemplate', array($foo, $bar)), or similar..think that would be amazing.

Main reason for this right now is that I'm conditionally adding js and css by adding styles and scripts to $js and $css arrays, which AIOM will then combine and minify, and it would be nice when I could add those files from the field template.

I read about css and js dependencies, but I don't really know how to use them and how I could add them to my $js and $css arrays..?

Maybe this is already what I want?

But passing custom variables to field templates could be nice anyways, what do you think?

Link to comment
Share on other sites

I am not sure about passing variables to field->renderValue.

But adding your JS and CSS dependencies to custom arrays is pretty straightforward.

In config.php you can define your custom JS and CSS arrays like

$config->customJs = new FilenameArray;
$config->customCss = new FilenameArray;

Then in your template file where you want to include your dependency you do

$config->customJs->add('pathtoyourscriptfile');
$config->customCss->add('pathtoyourstylefile');

In the head section of your html you then render them with

 <?php if(count($config->customCss)) foreach($config->customCss->unique() as $file) echo "\n\t<link type='text/css' href='$file' rel='stylesheet' />"; ?>

and in the head or end of body section render your JS

<?php foreach($config->customJs->unique() as $url) echo "<script src='$url'></script>"; ?>
  • Like 3
Link to comment
Share on other sites

Right now I switched this particular case over to TemplateFile..

But thanks for this great insight gebeer! Didn't even know about this :-)

Can't get it working with AIOM where I need a "normal" one dimensional array

$config->customJs has the array within "data" key

$config->customJs->getIterator() doesnt work either

and ->data is protected...

By the way while investigating the source, Ryan commented the unique method and states that it's no longer necessary, add() is taking care of this already ;-)

To "inject" custom vars into renderValue, should be possible anyways to "create" new wire() vars like wire('js', array()) (or wire()->set('js', array())), then it should be possible to $wire->js[] = 'foo' from the field template, and almost anywhere else..

  • Like 1
Link to comment
Share on other sites

Can't get it working with AIOM where I need a "normal" one dimensional array

$config->customJs has the array within "data" key

$config->customJs->getIterator() doesnt work either

and ->data is protected...

This might be related to the directory traversal setting in AIOM. I stumbled over this once.

By the way while investigating the source, Ryan commented the unique method and states that it's no longer necessary, add() is taking care of this already ;-)

Thank you for this hint. I wasn't aware of it.

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...