I have a Django forms dilemma. When rendering a form in a template, I usually prefer to print the form fields individually, in stead of using the form.as_p annotation. However, in the form I am working on I have a set of fields that are repeated an unknown number of times. For example, if there are 3 items being edited the fields would be:
field_name_1
field_email_1
field_name_2
field_email_2
field_name_3
field_email_3
But the number of items in not known to me beforehand. To solve this I am creating the fields in a loop in the init function, no problems there. But I can't figure out a way to print the fields individually in the template, since I don't know the whole name of each field. If this were my only problem I could just use the form.as_p syntax, no biggie. But I would also like to add some html to the beginning of each set of fields, so that the result would be:
Add values 1:
field_name_1
field_email_1
Add values 2:
field_name_2
field_email_2
Add values 3:
field_name_3
field_email_3
However, I haven't found a way to insert arbitrary html into a form in Django code, similar to #markup in Drupal forms. So:
Is there a way to print the fields individually so I can put the extra html in the template? (What I am missing here is the exact syntax)
If not, how do I insert the extra html into the form?
Do you mean something like this in the template?:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
(from the Django documentation: https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-hidden-and-visible-fields )
You could render the header with something like:
Add values {{forloop.counter}}:
Related
I am dealing with 2 variables here. 1st being $xyz which contains all the values I would like to bind to the form and 2nd being $fields which contains all the input fields.
I am looping through $fields->field_name to show all the form fields from the database.
But 1 of the fields being an image, which is a different case. I am simply trying to bind manually the image url to the img element here.
To successfully bind an image. I need 2 things, 1 being the field name which I can get it from $fields->field_name and second is the url which is in $xyz.
so, I plan to do something like: $xyz->get($fields->field_name). This means, I try to access $xyz object and get dynamic image field name. but not sure why my below code throws error:
<img src=" {{ url($xyz->get($fields->field_name)) }}" />
code like above works completely fine in Controller, not sure how to achieve this in Blade front-end Views.
error:
Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given
I found the solution by myself:
below works, instead of using get:
{{ $xyz[$fields->field_name] }}
I am building a form using Form Model Binding via the Laravel Collective HTML package. The documentation (https://laravelcollective.com/docs/5.2/html#form-model-binding) boasts how the model's value is conveniently available with the value being set in the priority order of:
Session Flash Data (Old Input)
Explicitly Passed Value
Model Attribute Data
This is super useful because if a user has changed multiple fields, but 1 of them fails validation, you don't want all the other fields being reset to original value when they get thrown back to the form page with a helpful Message Bag of errors to give them a chance to correct their invalid input.
It's all very well using the Form::label, Form::text and Form::select helpers to leverage this lovely feature but what if you just want to access that convenient variable directly to do something a bit left-field?
Here's what I have tried...
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::value('first_name') }} <-- No
{{ Form::session('first_name') }} <-- Nope
{{ Form::input('first_name') }} <-- Still no
{{ Form::attribute('first_name') }} <-- Absolutely not
{{ Form::close() }}
Help me, what is the method I am looking for?
The documentation hints at the ability to get the thing I want here: https://laravelcollective.com/docs/5.2/html#custom-macros where it says it's "easy" to define your own custom Form class helpers called "macros" but it doesn't actually tell you where you would write the code for this or where in the code you can look if you want to poke around and learn more about the FormBuilder class?
The method for doing this is Form::getValueAttribute('first_name') and the location for poking around to learn more is /vendor/laravelcollective/html/src/FormBuilder.php
Is it possible to offload frontmatter to an include in some manner? My Jekyll site uses a frontmatter variable for the page title, however I have some pages that share an include due to code repetition. Putting the frontmatter for the page title in the include treats it like raw HTML. Is there a way to simulate this and set variables in an include?
According to the Jekyll docs on passing parameter variables to includes, you need to capture your front-matter as a variable first.
Their example uses capture, but you can use assign if you're just passing the variable through. Here's my tested example of how to do this:
include
<div>
{{ include.title }}
</div>
include in content
{% assign include-title = page.title %}
{% include example.html title=include-title %}
There is no front matter in includes.
But you can reach page's title from inside an include by using page.title, and any other page's variable with page.myVarName.
You can pass a parameter to an include. Try:
{% include name.html var={{page.title}} %}
Then in the include, access the parameter using include.var.
See "Pass Parameters to Includes" at https://jekyllrb.com/docs/includes/
I'm using Silex, and need to alter a Twig file based on the location its loaded from.
My first thought was checking the url, but unfortunately this returns the location of the rendered Twig location, and not the parent url as expected.
{{ path(app.request.attributes.get('_route')) }}
Another attempt was to pass some vars with the render, however I'm not certain how to do this from within Twig (and if it's possible at all).
{{ render(path('cart')) }}
Can someone possible help me with a solution?
Thanks in advance
This is indeed a problem with silex core. I have submitted a pull request that introduces a new renderRoute twig function that should fix the issue. Hopefully it will be merged soon.
Well i didn't get perfectly what are you asking, but if you want to pass variables in render method you can do this:
{% render "ProjectTesteBundle::ControllerName::actionName" with { 'variableNameToPass' : variableValue } %}
if you want get route name in twig template you can do do something like this:
{% set routeName = app.request.attributes.get('_route') %}
I want to pull a url segment variable into the pyrocms file plugin call. it would look like
{{files:listing folder="[segment(2)]"}}
or something of the sort. What is the trick for embedding
{{url:segments..}}
inside
{{files:listing folder="…}}
I am trying to setup this up for a conditional query for a photo gallery
If you take a look at the PyroCMS Tags documentation you will see this clearly documented in the "Tag Attributes" section.
You may also use the output from other tags as attribute values in your tags. For example if you wanted the url segment to default to the slug of the currently viewed page you could do this:
{{ url:segments segment="1" default=page:slug }}
Here is an example showing the proper use of quotes and braces when the tag used as the attribute value has an attribute itself.
{{ url:segments segment="1" default={foo:bar value="baz"} }}
Tip: Omit quotes and braces when using tags as attribute values. The only exception is when the tag you are using as the attribute value has its own attributes.
So you can do that easily with:
{{ files:listing folder={url:segments segment="2"} }}
Basically you don't need to pretend it's a string if it's not. You can just send the foo:bar arguments through, but it if has attributes you can "group" the attributes with the call via a single { and }.
Simple right? :)