I have a render array for a form in my D7 module that looks like this:
'form' =>
array
'#id' => string 'demo-form' (length=9)
'#action' => string '/sprint07/' (length=10)
'name' =>
array
'#title' => string 'Username' (length=8)
'#maxlength' => int 13
'#size' => int 15
'#type' => string 'textfield' (length=9)
'#required' => boolean true
'#input' => boolean true
'#autocomplete_path' => boolean false
... snip ...
I'm using twig-for-drupal to create a theme, but I can't seem to get the values back, I've tried {{ form['name']['#type'] }}, {{ form.name.#type }}, {{ form.name['#type'] }} and so on, but they all come back blank.
Other fields such as form['#action'] works fine, so how should I access the information more than one array in? I can't change the module too much as the php template in its native state uses render(form);
-- Update --
Added some more debugging to this, and seems that I can use {{ form|dump('v') }} to show me the whole array as above, but when I do {{ form.name|dump('v') }} I just get a string, which I assume means that twig is automatically detecting its a render array, and "helpfully" converting it for me.. So I guess that makes my question: how do I turn off the auto-rendering?
"name" sub array don't contain the "#id" key.
try if work
{{ form.name.#title }}
Turns out it isn't currently possible to do this on a case-by-case basis, TFD is currently set up to auto-render anything it thinks is a render array, and to turn this off you need to set autorender to FALSE in twig_get_instance(). I've tried this myself however and it still seems to be turning the array into a string, so this could be a bug within TFD, or I just don't understand the usage, which is equally, if not more, likely.
From ReneB's sandbox:
Autorender
This version of the TWIG engine uses auto render to prevent themers get RSI from typing {{node.field_somefield|render}} for every single field they want to render from the render array (of doom) so the can safely type {{node.field_something}}
On rendering of the compiled template TFD check if the called variable is a string, callable or array. If it's a string it simple does echo $string, if it's a callable it return a proper method() for it. And if it's an array, it assumes it's a renderable array and maps it to the render($string); method of drupal.
This way the objects hidden with hide() are respected.
To turn this is for now, you have to set autorender to FALSE in the twig_get_instance() method of twig.engine.
I'am working on a {% noautorender %} {% end noautorender %} block structure.
Related
I can't figure out why that when trying to add a custom post meta value from a set of checkboxes, by default a nested array set is being created, where I would just have a single array of the values I've saved.
So when I perform add_post_meta($post_id, 'Procedure', $_POST['bv_procedures']); and then do a var_dump of the value, I am returned with this:
array (size=1)
0 =>
array (size=2)
0 => string '13419' (length=5)
1 => string '13416' (length=5)
Why is the first index there, and how can I just have a single array containing the two values?
-- UPDATE --
I have since noticed somewhat strange behaviour with this now:
var_dump(get_post_meta($post->ID, 'Procedure', true)); will result in this being displayed on the screen when editing this post:
array (size=1)
0 => string '13419' (length=5)
However, I'm looking in the 'Custom Fields' section, and Procedure is not listed in there at all, whilst other post meta pairs are. So I have no idea what's going on here, why is this not being listed? And also, I don't understand why stating get_post_meta to return a string value, returns an array (albeit not nested). Surely, this is a bug?
In mustache:
I have 'matches'=>['foo', 'bar']. I also have:
[
'deals'=> [
'foo' => new Deal('name1'),
'bar' => new Deal('name2'),
'baz' => new Deal('name3')
]
]
What I am trying to do is this:
{{#matches}}
{{deals}}.{{.}}.{{name}}
{{/matches}}
Which doesn't work.
This works, except it isn't dynamic like I need:
{{#matches}}
{{deals.bar.name}}
{{/matches}}
Any thoughts or suggestions?
You may want to make a projection ahead of time that filters deals on matches in code before applying it to the template. If I'm understanding right, you're attempting to embed matching logic in the template which Mustache doesn't generally support.
You could either filter matches, or apply a Boolean property to each describing whether it has a match.
I kind of prefer using PHP Template Engine to Twig in symfony2 due to some flexibility, and something I am more used to.
For instance, I will like to use things like 'ternary If' when printing out some stuff.
My problem now is that, My data got rendered on the template as Objects. using (array) seems not to help as well as I ended up getting things like;
array (size=4)
'�news\newsBundle\Entity\News�id' => int 1
'�news\newsBundle\Entity\News�title' => string 'News title 1' (length=12)
'�news\newsBundle\Entity\News�body' => string 'Some body text' (length=14)
'�news\newsBundle\Entity\News�createdDate' =>
object(DateTime)[306]
public 'date' => string '2014-11-05 19:41:48.000000' (length=26)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/London' (length=13)
How do I get results like a normal array e.g array('id'=> int 1) without the namespace stuff in it.
Thanks in advance
You may want to change hydration mode in your queries to get entities as arrays:
http://doctrine-orm.readthedocs.org/en/latest/reference/dql-doctrine-query-language.html#array-hydration
Symfony2, Doctrine 2: getResult Object
Hello I'm currently working with Laravel 4 forms. I'm struggling to generate a text input with a specific class without choosing a 'default value'. I want to do the following:
{{ Form::text('first_name', array('class' => 'first_name')) }}
However I get this error (htmlentities() expects parameter 1 to be string, array given.) unless I add a default value:
{{ Form::text('first_name', 'Some Value', array('class' => 'first_name')) }}
The default value then populates the field and needs to be deleted before entering a new value. So it can't even be used like a place holder.
Thank you in advance,
Dan
Instead of a value, supply null. (do no supply empty string "")
This will come in handy in the future if you are going to work with Form Model Binding (http://laravel.com/docs/html#form-model-binding) because null will give the value of the given model attribute.
You can pass an empty value "" like,
{{ Form::text('first_name', '', array('class' => 'first_name')) }}
Because Laravel 4's HTML Form Builder API will accept first parameter as name, second parameter as value which is null by default and the third parameter as options array which is an empty array by default.
So basically you can build text input by passing only name like,
{{ Form::text('first_name') }}
And if you are planning to pass options which is the third argument, you must pass second argument also.
See API Doc here http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#235-246
I found it better to use the Input::old('first_name') for your default value instead of just "", like:
{{ Form::text('first_name', Input::old('first_name')) }}
This way, if you go back to the form with invalid data and pass the form input, it will reload the old input that the user previously inputted. In this case, first_name is/can be bound to the first_name field in your database table as well.
Edit: Yes, the third option is an array of input options, such as text field id, size, etc. or any other HTML attribute.
Keenan :)
For a site I am working on I use a library to get a list of states. It returns a numerically indexed array of states, each with three keys: stateCode, stateName, and stateSeg. It looks like this:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
array
'stateCode' => string 'AZ' (length=2)
'stateName' => string 'Arizona' (length=7)
'stateSeg' => string 'arizona-az' (length=10)
I often find myself with one of the three values and needing to look up its corresponding value. To do this I find myself constantly having to iterate through the array of states to find the data I need. Like this:
foreach ($this->data['stateList'] as $state)
{
if ($state['stateCode'] == $searchParams['state'])
{
$stateSeg = $state['stateSeg'];
break;
}
}
$url = BASEURL . '/' . $stateSeg . ".html";
This seems inefficient to me. I think the most efficient solution I’ve been able to come up with is to turn states into objects and put them in array with multiple keys for stateCode, stateSeg, and stateName each pointing to the same state object, so they can be referenced like this:
stateList[‘CA’]->getStateSeg();
or
stateList[‘Arizona’]->getStateCode();
or
stateList[‘alaska-ak’]->getStateName();
etc…
This also seems like kind of a hack which would result in a rather large array (150 keys pointing to 50 objects) with replicated data (keys replicating data stored within objects).
Anyway, just thought I’d see if there is some kind of pattern for this type of problem. This array of states isn't the only thing I’ve come across where I’ve had to do this sort of iterative searching on multidimensional arrays to find corresponding values.
Question is tagged PHP and the code above is in PHP, but I am interested in elegant solutions in any language.
If php supports references and I know the state, I'd just pass a reference to the appropriate array element and extract from it the necessary field.
Alternatively, if you never know in advance what state you can get, create and use a map (associative container/array), let its efficient implementation take care of quickly finding whatever you need. Seems like you may need several of them.
Also, I wonder if you could get rid of everything except the "alaska-ak" strings. The data appears highly redundant.
I think your basic idea with the object and the arrays is not that bad, but instead of creating actually objects, I would just refer to the existing objects (better: array data). Let's see your original list again:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
...
Each state object has an identifier, the array key: 0, 1, 2, ... .
All you need to do is to create three indexes based on key. You use the value as key (e.g. "AL" for "stateCode" index) and as value you take the array index, 0:
$indexStateCode['AL'] = 0;
You can then already look this up quickly:
$states[$indexStateCode['AL']];
Encapsulate this into a class with ArrayAccess and then on request instantiate the state object. You don't need it earlier.
Could you store the states in a mysql/sqlite table and use the database engine to do the lookup?
This seems inefficient to me
It isn't. Even worse-case, iterating through 50 items is probably an order of magnitude faster than querying a db.
a library to get a list of states
Not sure why you'd need a library to do this. But I'd either change the library to return the array how you need it, or wrap it in another module.
The data is somewhat redundant... All you need is two items: the state code and the state name. You can construct the "state seg" from those two. So keep a state code map and a state name map.