Let's say I have created a button component I want to include into my project with Sage 10's #include function, how do I add fallbacks to the variables, in case I or another user forget to add a value?
My button*:
<button class="<?= esc_attr($button_class); ?>">
<?= esc_html($button_label); ?>
</button>
My include
#include('partials.button', [
'button_class' => 'btn btn-primary',
'button_label' => 'label',
])
How can I add fallbacks, so button isn't empty if I don't add button_label?
*Code is simplified for this example
In the external PHP-file, with the button, you can add the variables between some PHP-tags, to catch any empty variables and instead return a fallback.
In this case, the PHP-file with the <button> element would look like this:
<?php
$button_class = $button_class ?: 'lg:block w-full lg:w-auto btn btn-primary mr-4';
$button_label = $button_label ?: __( 'Insert text here', 'fallback', 'edc' );
?>
<button class="<?= esc_attr($button_class); ?>">
<?= esc_html($button_label); ?>
</button>
Sage will first apply the variables from the #include-part to the variables in the top, after which PHP uses the value as is after the closing ?>-tag.
Related
I want to display multiple charts in a single page or different pages. How can I reuse the blade file instead of repeating/retyping the code?
I created a plain blade file _chart-widget.blade.php and I want the variable value to change depending on the page, or depending on what I want to set the variable in each <section> of a page
<!--begin::Charts Widget 1-->
<div class="card {{ $class ?? '' }}">
<!--begin::Header-->
<div class="card-header border-0 pt-5">
<!--begin::Title-->
<h3 class="card-title align-items-start flex-column">
<span class="card-label fw-bolder fs-3 mb-1">Recent Statistics</span>
<span class="text-muted fw-bold fs-7">More than 400 new members</span>
</h3>
<!--end::Title-->
<!--begin::Toolbar-->
<div class="card-toolbar">
<!--begin::Menu-->
<button type="button" class="btn btn-sm btn-icon btn-color-primary btn-active-light-primary" data-kt-menu-trigger="click" data-kt-menu-placement="bottom-end">
{!! theme()->getSvgIcon("icons/duotune/general/gen024.svg", "svg-icon-2") !!}
</button>
{{ theme()->getView('partials/menus/_menu-1') }}
<!--end::Menu-->
</div>
<!--end::Toolbar-->
</div>
<!--end::Header-->
<!--begin::Body-->
<div class="card-body">
<!--begin::Chart-->
<div id="kt_charts_widget_1_chart" style="height: 350px"></div>
<!--end::Chart-->
</div>
<!--end::Body-->
</div>
<!--end::Charts Widget 1-->
How can I make the code above dynamic and reusable when I #include it?
You can include views in Laravel blade template.
here you can read more.
Just use like this:
<div>
#include('_chart-widget')
</div>
If you need to pass data to your widget component, just give parameters as an array to your component:
#include('view.name', ['status' => 'complete'])
If you want variables to be different in each page simply pass vairables from Controller.If you are on the same page and including same blade multiple times this can help you:
#include('view.name', ['code' => 'complete'])
This will set different values for $code variable in different sections.
Check out documentation here.
I want to do a search using a form which will search name field, brand field and category field.
Should I be using post or get for this situation?
How/where should I be creating my query search model, model or controller?
Any code snippets would be great.
I have the following in my view:
<?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'get', 'action' => Url::to(['productitem/search'])]); ?>
<?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
<?= $form->field($productitem, 'brand_id')->dropDownList(
ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
['prompt'=>'Select Brand']
)->label(false) ?>
<?= $form->field($productitem, 'category_id')->dropDownList(
ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
['prompt'=>'Select Department']
)->label(false) ?>
<div class="form-group search-button">
<button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
</div>
<?php ActiveForm::end(); ?>
The answer to your questions:
I would use get just for the convenience of pressing the "back" button. You are not changing the database at all so a GET should provide enough security / convenience
search model should be in the [Model]Search. Yii2 has specialized search models on top of the normal models why not keep them there? That is exactly what they are there for.
No code snippets SO helps you with your code, does not usually writes it for you. But then again somebody will probably come and spoon feed you code anyway so you will not learn anything so enjoy.
I have a file input in my ActiveForm and I want to style it
but classes btn btn-primary doesn't have any effect on that
<?php
use yii\widgets\ActiveForm;
?>
<div class="jumbotron">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($model, 'file',['class'=>'btn btn-primary'])->fileInput() ?>
<button class="btn btn-default">Submit</button>
<?php ActiveForm::end() ?>
</div>
At this point everything is well fine. However, when I try to use the parameter options of the field method as follows:
<?= $form->field($model, 'file',['class'=>'btn btn-primary'])->fileInput() ?>
I have got the error:
Class btn btn-primary does not exist
but I used 'btn btn-primary' class in
<button class="btn btn-default">Submit</button>
and it worked very well.
Could anybody explain me why this error has been occurred?!
In total approach you can't style file button directly
You can make the primary file input hidden and use another button.
When this button clicked, you must trigger the click event of input file using js or jquery
Or you can write it like below
<?= $form->field($model, 'file')->label(null,['class'=>'btn btn-primary'])
->fileInput(['class'=>'sr-only']) ?>
I want to change this:
{{ Form::submit('Delete this User', array('class' => 'btn btn-warning')) }}
to something like this:
{{ Form::submit('<i class="glyphicon glyphicon-delete"></i>', array('class' => '')) }}
I know that the second option isn't correct, anyone knows how to write it in a correct way?
Use a <button> of type submit, which adds more flexibility then a submit input:
{{Form::button('<i class="glyphicon glyphicon-delete"></i>', array('type' => 'submit', 'class' => ''))}}
To make it clear, this is the class method:
public function button($value = null, $options = array())
{
if ( ! array_key_exists('type', $options) )
{
$options['type'] = 'button';
}
return '<button'.$this->html->attributes($options).'>'.$value.'</button>';
}
As you can see, $value holds anything you put inside the <button> tag, so placing the icon there should work - I use this with Fontawesome icons and it works fine, I personally tend to use those instead of Glyphicon but the principle remains the same.
By using Form::submit(), instead, you create an <input type="submit" which cannot accept html as content of the value attribute, that's why your solution won't work.
This works for me
{{ Form::button('<span class="glyphicon glyphicon-remove"></span>', array('class'=>'btn btn-default', 'type'=>'submit')) }}
I used Html::linkRoutes to create a button and cannot implement the methods you guys mentioned before... Any one have any guesses? Heres my code:
<div class="col-md-12">
{{ Html::linkRoute('posts.index', '<< Back to blog', [], ['class' => 'btn2 btn2-default']) }}
</div>
You can add your icon class to your class array like:
array('class' => 'btn btn-primary glyphicon glyphicon-remove');
I'm trying to use some bootstrap features like the Icon glyphs in the Yii CHtml class, here is my code:
<?php
echo CHtml::submitButton('<i class="icon-user"></i> Login',
array(
'class' => 'btn btn-large pull-right'
));
?>
But it kinda don't "recognize" the tag and just renders the tag out like the image bellow .
does anyone knows how to workaround it (without typing the html tags itself).
Thank you guys.
CHtml::submitButton produces an <input type="submit"> that cannot accept additional HTML as its content. However, you can do things to taste with CHtml::tag:
echo CHtml::tag('button',
array('class' => 'btn btn-large pull-right'),
'<i class="icon-user"></i> Login');
This will produce a <button> tag that can take arbitrary HTML as its content.
Update: As frostyterrier points out in the comments, there's a built-in method CHtml::htmlButton that allows you to do this even more easily:
echo CHtml::htmlButton('<i class="icon-user"></i> Login',
array('class' => 'btn btn-large pull-right'));
Try to set 'encode' to false in htmlOptions parameter.
<?php
echo CHtml::submitButton('<i class="icon-user"></i> Login',
array(
'encode' => false,
'class' => 'btn btn-large pull-right'
));
?>
CHTML::submitbutton generates <intput type="submit" /> tag and you're trying to cram HTML into its value attribute.
Why don't you simply add the icon-user CSS class to the button itself?
echo CHtml::submitButton('Login', array(
'class' => 'btn btn-large pull-right icon-user'
));
If it's a Bootstrap-defined class, it should style the input button nicely.
try this
$this->widget("bootstrap.widget.TbButton", array(
'buttonType'=>'submit',
'label=>'Login',
'type'=>'success', //or default or warning... as you like
'icon'=>'user',
'size'=>'large', //small, mini, or just comment line for default
));
documentation: http://www.cniska.net/yii-bootstrap/#tbButton
Firstly you should change from subbmitButton to htmlButton,for example write the following code:
<?php echo CHtml::htmlButton('<span>Log in!</span>',array('type'=>'submit','class'=>'tbutton small pad')); ?>