yii2 include html code inside Html::button - yii2

I need to include some html code inside Html::button in yii2. According to the Class yii\helpers\BaseHtml i can pass html code to $content. Please, correct me if i am wrong. So i wrote:
<?= Html::button{'<div class=\'row\'></div>,[]); ?>
and it works. But what if the code i want to insert is big? How do i include big code with less pain?
Thank you very much.

You could use views and renderPartial
e.g. in your view
<?php
$html = $this->context->renderPartial('sub_view', [
'attribute' => 'test',
]);
echo Html::button($html,[]);
?>
and in your subview your static html or dynamic code
<h1>static html code<h1>
<div>
<?php
echo $attribute
?>
</div>

Related

Wordpress Polylang adding pll_e breaks HTML

i'm using polylang to translate my blog site along with loco translate. i'm manually adding string translations which was working fine with get_theme_mod parts, but there is a place that i want to also add custom string translation, after i add manually it breaks html and css won't work then.
it should be seen like this after adding custom string translation ; works fine without pll_e
but after i add pll_e to the that part in index.html ;
<?php get_header(); ?>
<div class="content">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<?php echo get_template_part(pll_e ('inc/page-title') ); ?>
<?php endif; ?>
it breaks the html but translation works. its seen like this ; looks like this
does anyone knows the solution ? i think its about get_template_part and get_theme_mod because the same things that i've done with get_theme_mod parts works fine.
by the way there is difference like this with pll_e and without it.
without pll_e
with pll_e
i solved the problem by editing index.html like this ;
<div class="content">
<div class="page-title group">
<div class="page-title-inner group">
<?php if ( get_theme_mod('heading-enable','on') == 'on' ) : ?>
<h2> <?php echo get_template_part(pll_e('inc/page-title') ); ?> </h2>
<?php endif; ?>
</div><!--/.page-title-inner-->

In Magento 2.1.5, is there a way to render a widget within a template by its ID?

I would like to render a widget in my template by its ID, just like I have with blocks:
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('block_name')->toHtml();
Try this. This will help you.
For Display Type = All Product Widget
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("all_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>
For Display Type = New Product Widget
<?php echo $this->getLayout()->createBlock("Magento\Catalog\Block\Product\Widget\NewWidget")->setDisplayType("new_products")->setProductsCount("5")->setTemplate("product/widget/new/content/new_grid.phtml")->toHtml(); ?>

Html::img tag not working in yii2

I am new to yii2 and I am trying to add images to my website with the help of Html::img tag but I keep getting broken links on the website. I am using wamp. This is my code:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
Thanks for the help.
You should conver to an url your alias
use yii\helpers\Url;
...
<?= Html::img( Url::to('#web/images/logo.png'), ['alt' => 'My logo']) ?>
Try this
<?php $domain = yii\helpers\Url::base(true);
echo Html::img($domain.'/images/logo.png', ['alt' => 'My logo'])
?>

How to use HTML::tag() method to display HTML5 tag?

I want to echo the following HTML tag:
<ul class="uk-grid" data-uk-grid-margin>
I investigated yii2 HTML helper sources. It seems there's no possibility to echo the tag with HTML::tag() method. Am I right?
For sure I can simply do:
echo '<ul class="uk-grid" data-uk-grid-margin>';
but I am curious about different possibilities.
What is the best solution?
yes you can
<?php echo Html::tag('ul', NULL, ['class' => 'uk-grid', 'data-uk-grid-margin' => ''] ); ?>

EntityMalformedException when retrieving Drupal 7 custom field

I am playing with Drupal and I am trying to add a second line to the site slogan.
The following is the piece of page.tpl.php where I am working.
<?php if ($site_name || $site_slogan): ?>
<div id="name-and-slogan" class="hgroup">
<?php if ($site_name): ?>
<h1 class="site-name">
<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>">
<?php print $site_name; ?>
</a>
</h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<p class="site-slogan"><?php print $site_slogan; ?></p>
<?php endif; ?>
<?php
/* ADDED */
$node = menu_get_object();
$siteslogan2 = field_get_items('node', $node, 'field_siteslogan2');
?>
<?php if ($siteslogan2): ?>
<p class="site-slogan2"><?php print $siteslogan2; ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
I basically added a new Content Type with a field called siteslogan2 (field_siteslogan2) and now I would like to retrieve and show it here.
The first problem is that the $node var is not defined (even if according to the documentation it should be). The second problem is that I receive EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() when I define it manually and run it.
First of all, is this the right approach to the problem? Secondly, why do I receive the EntityMalformedException and how can I fix it?
var_dump($node) produces NULL. It must be the way I get the $node content that is not good. The doc is a bit cryptic to me when it says:
$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).
This sort of thing is can be done using a preprocessing function in your theme's template.php to define and set a variable in the $varables array.
This variable then becomes available to whichever template ( page, node, form, etc ) you have done the preprocessing on. Your theme's template.php file will most likely have comments on how to do this.
for instance, doing this in the template.php creates or modifies the value of the variable $display_header, making it available for use in node.tpl.php
function yourthemename_preprocess_node(&$variables, $hook) {
$variables['display_header'] = false;
}
You can then modify the node template file to use this variable.
For something simple like a sub-slogan, you can add a setting to the theme, so it shows up on the theme's configuration page like any other theme setting.
This requires implementing this function in your theme's theme-settings.php:
function yourthemename_form_system_theme_settings_alter(&$form, $form_state) {}
This provides information to the settings form regarding the new setting. like this:
https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_form_system_theme_settings_alter/7
Hopefully that's enough to get you started.