I have designed a template using the template library of codeigniter, the template has the following regions:
$template['template_ar']['template'] = 'template_ar';
$template['template_ar']['regions'] = array(
'header',
'title',
'content',
'topcontent',
'sidebar',
'footer',
'options',
'script',
);
I used the following code to render my template
class faq extends MY_Controller {
/**
* Index
* This function views the Home Page
*
* #access public
* #return
*/
public function index() {
$this->template->write_view('content','comment/questions');
$this->template->write('options','You one',TRUE);
$this->template->render();
}
}
The problem that I want to eliminate is that I want to prevent the top content from appearing in my template. I need only the content, the header and the footer. Can anyone tell me how to do this?
Template Library for CodeIgniter
Set regions for writing to
$this->template->set_regions($regions);
Dynamically add region to the currently set template
$this->template->add_regions($name, $props);
Empty a region's content
$this->template->empty_region($name);
Related
Right now I am using a shortcode function add_shortcode in display contents in the wordpress site. Now I want to inject a code in the footer by not modify the theme or a child them, instead I will do it via plugin. I just do not know the correct function name to search in google. I always use add_shortcode because it will display on all pages. it is just a custom popup code by me but I have to place the code in the footer
I am not really sure what you are asking? Do you need to know how to actually write a plugin? If this is the case then there is plenty of tutorials covering this topic. Some good resources related to this:
https://developer.wordpress.org/plugins/
https://developer.wordpress.org/plugins/hooks/
https://developer.wordpress.org/reference/hooks/wp_footer/
Add good thing might be to check how an existing plugin which does this is structured, for instance:
https://wordpress.org/plugins/insert-headers-and-footers/#developers
Do add something in the footer on every page you would do something like this, this file should be added under the wp-content/plugins directory:
<?php
/**
* Plugin Name: Footer plugin
* Description: Adds text to footer
* Version: 1.0.0
* Requires at least: 3.0
* Tested up to: 6.0
* Author: Author
* Text Domain: footer-plugin
**/
class FooterPlugin
{
public function __construct()
{
// hook into wp_footer
add_action('wp_footer', [$this, 'addFooter']);
}
public function addFooter()
{
echo '<span>footer text</span>';
}
}
// initialize the plugin when everything is loaded
add_action('plugins_loaded', function () {
new FooterPlugin();
});
Another way of doing this would be to replace the content of the footer from the generated output for the page:
class FooterPlugin
{
/** #var string */
private $replacement = 'SOME TEXT';
public function __construct()
{
add_action('template_redirect', [$this, 'templateRedirect']);
}
/**
* #var string $content
**/
public function templateRedirect(string $content)
{
ob_start([$this, 'handleBuffer']);
}
/**
* Replace everything in the footer tag
* #param string $buffer
* #return string
**/
public function handleBuffer(string $buffer): string
{
return preg_replace('/(\<footer\s*.*\>)((.|\n)*)(\<\/footer\>)/', '$1' . $this->replacement . '$3', $buffer);
}
}
I want to create a CakePHP Widget in order to create a custom form control. The end goal is to make it a plugin, but for now I am trying to determine the general structure of a Widget. I have created a file in src/View/Widget/DateTimeWidget.php containing
<?php
namespace App\View\Widget;
use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
class DateTimeWidget implements WidgetInterface
{
protected $_templates;
public function __construct($templates)
{
$this->_templates = $templates;
}
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
];
return $this->_templates->format('DateTime', [
'name' => $data['name'],
'attrs' => $this->_templates->formatAttributes($data, ['name'])
]);
}
public function secureFields(array $data)
{
return [$data['name']];
}
}
?>
I load the Widget in a View with the code
$this->Form->addWidget(
'datetime',
['DateTime']
);
and then create a form control with it using
echo $this->Form->control('end_time', ['type' => 'datetime']);
However, I get the error Cannot find template named 'DateTime'.
I have created the basic template code
<?php
$this->Form->setTemplates([
'DateTime' => '<p {{attrs}}>Test template</p>'
]);
But I have no idea where in the folder structure to put it? In most plugins I have looked at it is in a helper file, but I wonder if this is the default way to do it? What are my options? And how do i tell CakePHP to load it? What is the preferred way of doing this?
Thank you!
If you want your widget to come with default string templates, then you could for example define them in the widget itself, by adding it to the string template instance that is being passed to the widget's constructor. You'd do it in the widget's render() method though, it wouldn't work properly in the constructor, as widget instances are being reused, ie they are only being constructed once, for example:
public function render(array $data, ContextInterface $context)
{
if (!array_key_exists('customDateTime', $this->_templates->getConfig())) {
$this->_templates->add([
'customDateTime' => '<p {{attrs}}>Test template</p>',
// ...
]);
}
// ...
}
Another option is to put the string templates in a config file:
// in path_to_your_plugin/config/form_helper_templates.php
<?php
return [
'customDateTime' => '<p {{attrs}}>Test template</p>',
// ...
];
and ask the users to load the form helper string templates in their view templates when they want to use your widgets:
$this->Form->templater()->load('YourPluginName.form_helper_templates');
Both options will integrate properly with the form helper, so that users can still override the templates by setting custom templates either via FormHelper::setTemplates(), StringTemplate::load()/add(), or the templates option for FormHelper::control().
I think you should use Cells for it.
Take a look at: https://book.cakephp.org/3/en/views/cells.html
I created a module with a Custom Widget. When logging into the admin, going to content->widgets, creating an instance, and setting that instance to appear on the homepage, the widget appears as expected.
I have removed this instance and am now trying to create the widget by going to content->pages, editing the homepage -> content section, and selecting insert widget. I created a new instance of the widget and added some additional text into the homepage -> content section.
When I load the homepage the widget does not appear, though the rest of the text does appear.
The block for the widget is being called ( I tested this with a var_dump and exit).
The template is not being rendered however.
Here is my Block:
<?php
namespace MyNamespace\Slider\Block\Widget;
use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Block\Product\Context;
class Slider extends Template
{
protected $_template = "Yamazaki_Slider::widget/slider.phtml";
/**
* #var CollectionFactory
*/
protected $_productCollectionFactory;
/**
* #var CollectionFactory
*/
protected $_imageHelper;
/**
* #param Context $context
* #param CollectionFactory $productCollectionFactory
*/
public function __construct(Context $context, CollectionFactory $productCollectionFactory)
{
$this->_imageHelper = $context->getImageHelper();
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
}
/**
* Retrieve featured products collection
*/
public function getProducts()
{
$collection = $this->_productCollectionFactory->create();
return $collection->addAttributeToSelect('*')->addAttributeToFilter('is_featured','1');
}
}
and my Template:
<?php $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct'); ?>
<?php foreach($this->getProducts() as $product): ?>
<?php $productImage = $imageBlock->getImage($product, 'product_page_image_large'); ?>
<?php echo $productImage->toHtml(); ?>
<?php endforeach ?>
The other files in my module are:
etc/widget.xml:
<?xml version="1.0" encoding="UTF-8"?>
<widgets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Widget:etc/widget.xsd">
<widget id="yamazaki_slider" class="MyNamespace\Slider\Block\Widget\Slider">
<label translate="true">My Image Slider</label>
<description>My Image Slider</description>
</widget>
</widgets>
module.xml and registration.php.
I do not have a etc/layout.xml file as I have noticed some widget/modules contain. Do I need this, or any other files?
Also, If I "Hide the Editor" inside content->pages->edit->content I see:
<p>{{widget type="MyNamespace\Slider\Block\Widget\Slider"}}</p>`
If I replace this with
{{block class="MyNamespace\Slider\Block\Widget\Slider" template="widget/slider.phtml"}}
The Content appears as expected
I managed to get the Block to Render
I had to add implements \Magento\Widget\Block\BlockInterface to MyNamespace\Slider\Block\Widget\Slider.
I'm not sure why this rendered, prior to adding the implements, using the first approach and not the second.
Magento is seriously lacking documentation!
I overrieded a controller in Prestashop 1.7 like this :
/override/controllers/front/MyAccountController.php
class MyAccountController extends MyAccountControllerCore
{
/**
* Assign template vars related to page content
* #see FrontController::initContent()
*/
public function initContent()
{
$this->context->smarty->assign([
'logout_url' => $this->context->link->getPageLink('index', true, null, 'mylogout')
]);
parent::initContent();
$this->setTemplate("module:configurateur/views/templates/front/my-account.tpl");
}
}
So I'm trying to call a view in my custom module "configurateur" with this line :
$this->setTemplate("module:configurateur/views/templates/front/my-account.tpl");
This file exists and is in the right folder (I think) :
\modules\configurateur\views\templates\front\my-account.tpl
When I try to load the page, I have this error :
No template found for module:configurateur/views/templates/front/my-account.tpl
at line 68 in file classes/Smarty/TemplateFinder.php
Can anyone tell my what's wrong please ?
The syntax "module:..." is only for ModuleFrontController objects, not for FrontController :
In your case your should use the hook DisplayOverrideTemplate or redirect the page myaccount to a module controller.
Im new to Yii2 and I have a need to modify the registerAssetBundle() function of kartiks FileInput widget in Yii2. I realize this is in the vendor folder, so I wanted to do an override. FYI, this is using the advanced template. Can anyone tell me why I cannot override or what Im doing wrong? Yii just doesnt pick this file up/ doesnt choke/ no errors/ nothing, just goes about its merry way and renders my page as normal.
In common\components I have a file called FileInputOveride.php:
namespace common\components;
use Yii;
use \kartik\file\FileInput;
class FileInputOveride extends \kartik\file\FileInput
{
//...override function, ...do stuff...
Edit -Heres some more code:
Here is the declaration at the top of _form.php which is using the fileInput
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\bootstrap\Modal;
use kartik\widgets\FileInput; <-- if I take this out, it errors that it cant find ::FileInput
use common\components\FileInputOveride; <--this has no effect
Below this line is some view html, until we get to the fileInput field which looks like this:
<?=
//fileinput widget for single file upload
$form->field($model, 'cover_file')->widget(FileInput::classname(),
[
'options'=>
[
'accept'=>'image/*',
'multiple' => false,
'id'=>'cover_file',
],
'pluginOptions' =>
[
'uploadUrl' => $upload_url,
'maxFileCount' => 1,
'allowedFileExtensions' => ['jpg', 'png','jpeg'],
'initialPreviewShowUpload' => false,
'uploadAsync'=> false,
'autoReplace'=>true,
],
'pluginEvents' =>
[
'fileuploaded'=>"function(event, data, previewId, index){
$.get( './call-image?id=".$model->id."', function( response ) {
$('#thumb-container-image').html(response);
});
}",
],
])->label(false);
?>
Trying to override the registerAssetBundle() function in this kartik FileInput.php with my own FileInputOveride.php:
namespace kartik\file;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use kartik\base\InputWidget;
use kartik\base\TranslationTrait;
/**
* Wrapper for the Bootstrap FileInput JQuery Plugin by Krajee. The FileInput widget is styled for Bootstrap 3.x with
* ability to multiple file selection and preview, format button styles and inputs. Runs on all modern browsers
* supporting HTML5 File Inputs and File Processing API. For browser versions IE9 and below, this widget will
* gracefully degrade to normal HTML file input.
*
* #see http://plugins.krajee.com/bootstrap-fileinput
* #see https://github.com/kartik-v/bootstrap-fileinput
*
* #author Kartik Visweswaran <kartikv2#gmail.com>
* #since 2.0
* #see http://twitter.github.com/typeahead.js/examples
*/
class FileInput extends InputWidget
{
and here is the entire FileInputOveride.php file:
namespace common\components;
use Yii;
class FileInputOveride extends \kartik\file\FileInput
{
/**
* Registers the asset bundle and locale
*/
public function registerAssetBundle()
{
$view = $this->getView();
if ($this->resizeImages) {
PiExifAsset::register($view);
$this->pluginOptions['resizeImage'] = true;
}
$theme = ArrayHelper::getValue($this->pluginOptions, 'theme');
if (!empty($theme) && in_array($theme, self::$_themes)) {
FileInputThemeAsset::register($view)->addTheme($theme);
}
if ($this->sortThumbs) {
SortableAsset::register($view);
}
if ($this->purifyHtml) {
DomPurifyAsset::register($view);
$this->pluginOptions['purifyHtml'] = true;
}
//above is the existing code
//below is the additional code i added to this function
$assetsRegistered = FileInputAsset::register($view)->addLanguage($this->language, '', 'js/locales');
//array of pages/paths we dont want to include the asset on
$pageArray = ['releases/update'];
//array of assets we dont want to use for the above pages
$fileArray = ['js/fileinput.js'];
//for each page, see if the file(s) specified is/are included, if so, unset them in the assets array
foreach($pageArray as $path)
if(in_array($path, $pageArray)){
foreach($fileArray as $file){
if(in_array($file,$assetsRegistered->js)){
$key= array_search($file, $assetsRegistered->js);
unset($assetsRegistered->js[$key]);
}
}
}
}
}
As an extra, I could also use the syntax to list the assets belonging to a action/view from within its action.
so:
public function actionUpdate(){
//show me all the js registered to this page
Thanks everyone!
In your _form.php file use "FileInputOveride::classname()" instead of "FileInput::classname()" - then you can remove use line for kartik input. When you extend any plugin then you must call your plugin class name instead of plugin you extend.