I created a variable inside a controller :
<?php
use Phalcon\Mvc\Controller;
class RestaurantreservationController extends Controller
{
public function indexAction(){
return $this->view->pick("reservation/listerReservation");
}
public function affecterReservTableAction($id) {
$this->view->action_form = 'affecterReservTableExec';
$this->view->titre = 'Réservation de table';
$this->view->table_code = $id; // here is the variable I want to manipulate
return $this->view->pick("reservation/reservTable");
}
}
?>
Inside the view reservTable.phtml I want to work with the variable table_code :
<div class="input-control select">
<select name="clt_id" id="clt_id">
<option value="">--Choisir une table--</option>
<?php
$table_code = {{table_code}}; // it generates an error
$tables = TableClient::lireParCritere([]);
foreach ($tables as $table) {
$select = ($table_code == $table->table_code ? "selected" : "" );
?>
<option <?php echo $select; ?> value="<?php echo $table->table_code; ?>"><?php echo $table->noms; ?></option>
<?php
}
?>
</select>
</div>
How can I use it to set as the selected element of the select element ?
The problem is you are mixing phtml and Volt syntaxes when you try to assign {{table_code}} to $table_code.
The Volt variable {{ table_code }} is the same as $table_code.
If you use Phalcon then it is good to use VOLT
When you create Volt type of template you can access variable by {{ table_code }}
If you wan't loop you can use something like
{% for table in tables %}
//do something
{% endfor %}
Volt has also nice function to create selects
{{ select("table", table_code, 'using': ['table_code', 'table_noms']) }}
Volt documentation
Select in Volt
You used it correctly three lines further down the code. At the point you're trying to use the variable, you're in PHP mode. That means you just write PHP code, i.e. $table_code. The form {{table_code}} is for interpolation inside a VOLT template, not for use inside PHP code.
I would actually recommend converting all of that PHP block into VOLT template code.
Related
Hi I'm new to October cms . I have defined the below shown method in my model class. The method is also used to show the select options in the backend form. This method returns an array where the key is a value similar to a field value in db. I have defined the method as static because it is also recommended in the front end where I get the function and process it with db record and iterate it to show the value of the array that matches the key. It all works fine. Thing is in my columns.yaml file, how do I list the method's array value which matches the db record as I did in front end.
public static function getSampleOptions()
{
return[
'1'=>'Sample1',
'2'=>'Sample2'
];
}
Hello friends I found the answer with some help from October CMS Help/Support
http://octobercms.com/index.php/forum/post/dropdown-shows-its-value-than-key-name-in-list-controller
and referred few concepts of laravel.
Model Class Method
public static function getSampleOptions()
{
return[
'1'=>'Mobile App',
'2'=>'Web App'
];
}
Columns.Yaml file
sample:
label: Sample Column
type: dropdown
Again back in the model, declare the attributes object and include the filed name as key with empty value
public $attributes = ['sample'=>''];
Define the getfield_nameAttribute() function to set the associated value for the appropriate key in the column
public function getSampleAttribute()
{
$result = $this->attributes['sample'];
$options = $this->getSampleOptions();
foreach($options as $key=>$value)
{
if($key == $result)
{
return $value;
}
}
}
Updated
The solution to rectify the problem while editing the record is simple.
Create a partial and modify the fields. yaml
_sample_options.htm (partial) // file name should begin with_(underscore)
<?php
$fieldOptions = $model->getSampleOptions();
$sample = $model->attributes['sample'];
?>
<select id="<?= $field->getId() ?>" name="<?= $field->getName() ?>" class="form-control custom-select" <?= $field->getAttributes() ?>>
<?php foreach($fieldOptions as $key=>$label)
{
?>
<option value="<?= $key ?>" <?php echo ($sample == $key)?"selected":''; ?>><?= $label ?></option>
<?php
} ?>
</select>
Here the $model and $field are the partial variables that are used to access the intended model's methods and properties.
Documentation : https://octobercms.com/docs/backend/forms#field-partial
Fields.Yaml file
sample:
label: Sample Field
type: partial
path: $/october/demo/controllers/sample/_sample_options.htm //path where the partial is located in the controller view
break a for loop in october cms template if condition not satisfied?
{% for key, pTest in pack.products %}
{{loop.length}}
<li>{{pTest.productable.name}} {{ key }}</li>
{% if key == 2 %}
<li class="more">...</li>
{% endif %}
{% endfor %}
I have this blade view template
<title><?
$page = App::make('page');
echo $page->getTitle( );
?></title>
<?php
$css = $page->getCssList('all');
foreach($css as $item){?>
{{ HTML::style('<?=$item['loc'];?>', array('media' => '<?=$item['media'];?>'))}}
<?}
$js = $page->getJsList('all');
foreach($js as $item){?>
{{ HTML::script('<?=$item['loc'];?>')}}
<?}?>
It does not work. What happens is it actually outputs the blade commands to the screen.
If i cut and paste these blade command into the view then they are rendered correctly.
What is the problem? Is is not possible to create dynamic blade commands? How can I solve this problem?
You are using PHP inside of PHP.
Change this
{{ HTML::script('<?=$item['loc'];?>')}}
to this
{{ HTML::script($item['loc']) }}
and change
{{ HTML::style('<?=$item['loc'];?>', array('media' => '<?=$item['media'];?>'))}}
to
{{ HTML::style($item['loc'], array('media' => $item['media'])) }}
The curly braces are simply a shortcut for printing PHP code.
You can just write
{{ HTML::script($item['loc']) }}
Which is functionally identical to
<?php echo HTML::script($item['loc']); ?>
maybe someone know, how can I get variable with name "foo" from blade template get to static function which is used in this template.
For example I have:
<div class="collumns large-8">
{{ Helpers::setLetters('variable_name') }}
</div>
<div class="collumns large-8">
{{ $variable_name }}
</div>
Both divs should set same string because in setLetters function will be
return ${$name};
But, that should be instead $name, because in case about, will be error.
I use laravel 4.1
This doesn't go in your View, it should be in your controller.
Controller
$name = Helpers::setLetters('variable_name');
return \View::make('yourview', array( 'variable_name' => $name);
controller::
$states=$this->States->get_States();
$data=array('states'=>$states);
echo View::make('frontend.list-property')->with('data', $data);
list-property.blade.php:: // this is sub view
#extends('layouts.frontend.frontend_login')
#section('content')
<select>
{{$states_drpdown}} //when I use this statement I am getting error
</select>
#stop
frontend_login.blade.php //This is layout file
<?php
$states_drpdown='';
foreach ($data['states'] as $state):
$states_drpdown.='<option value="'.$state->sid.'">'.$state->statename.'</option>';
endforeach
?>
<select>
{{$states_drpdown}} //I am getting list of options here
</select>
#yield('content')
I am getting following error, can any one help me out pls.
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Method Illuminate\View\View::__toString() must not throw an exception
You may also share a piece of data across all views:
View::share('name', 'Steve');
You can find more about this here.
In your case, try: (warning untested code)
$states = $this->States->get_States();
$data = View::share('states', $states);
return View::make('frontend.list-property');
$data = array('states' => $this->States->get_States());
echo View::make('frontend.list-property', $data);
Assuming $this->States->get_States() returns a string of options, not an array?
<select>
{{$states}}
</select>
If it $this->States->get_States() returns an array, you could just do this (no need for <select> tags):
{{ Input::select('name-of-field', $states, Input::old('name-of-field')) }}
Consider returning the view instead of echoing.
I have a variable suppose that is:
$menustr; this variable contains code html and some twig parts for example:
$menustr .= '<li><a href="{{ path("'. $actual['direccion'] .'") }}" >'. $actual['nombre'] .'</a></li>';
I need that the browser take the code html and the part of twig that in this momen is the
"{{ path(~~~~~) }}"
I make a return where i send the variable called "$menustr" and after use the expresion "raw" for the html code but this dont make effective the twig code.
This is te return:
return $this->render('::menu.html.twig', array('menu' => $menustr));
and here is the template content:
{{ menu | raw }}
Twig can't render strings containing twig. There is not something like an eval function in Twig1..
What you can do is moving the path logic to the PHP stuff. The router service can generate urls, just like the path twig function does. If you are in a controller which extends the base Controller, you can simply use generateUrl:
$menuString .= '<li>'. $actual['nombre'] .'</li>';
return $this->render('::menu.html.twig', array(
'menu' => $menuString,
));
Also, when using menu's in Symfony, I recommend to take a look at the KnpMenuBundle.
EDIT: 1. As pointed by #PepaMartinec there is a function which can do this and it is called template_from_string
You can render Twig template stored in a varible using the template_from_string function.
Check this bundle: https://github.com/LaKrue/TwigstringBundle
This Bundle adds the possibility to render strings instead of files with the Symfony2 native Twig templating engine:
$vars = array('var'=>'x');
// render example string
$vars['test'] = $this->get('twigstring')->render('v {{ var }} {% if var is defined %} y {% endif %} z', $vars);
// output
v x y z
In your case i would be:
return $this->render('::menu.html.twig', array(
'menu' => $this->get('twigstring')->render($menustr, $vars)
));