I am new to cakephp. I am trying to build home page with carousel.But I want to make the image in the slide get called from the slides table database.
Below is my carousel code in home.ctp
<div class="mbr-box mbr-section mbr-section--relative mbr-section--fixed-size mbr-section--bg-adapted item dark center mbr-section--full-height active" style="background-image: url(<?= ($slide->thumbnail) ?>);">
<div class="mbr-box__magnet mbr-box__magnet--sm-padding">
<div class=" container">
<div class="row"><div class="col-sm-8 col-sm-offset-2">
<div class="mbr-hero">
<h1 class="mbr-hero__text">Slide 2</h1>
<p class="mbr-hero__subtext">Description</p>
</div>
<div class="mbr-buttons btn-inverse mbr-buttons--center"><a class="mbr-buttons__btn btn btn-lg btn-danger" href="#">HERE</a></div>
</div></div>
</div>
</div>
</div>
style="background-image: url(<?= ($slide->thumbnail) ?>);" is the code that i thought could call the image file from database(images/17860481920x1200-165.jpg). But all i got is a blank.
Can anyone help me. Sorry for my bad english.
Easy way:
cake bake cell Carousel
this create two files,
first CarouselCell.php in View/Cell
<?php
namespace App\View\Cell;
use Cake\View\Cell;
/**
* Carousel cell
*/
class CarouselCell extends Cell
{
/**
* List of valid options that can be passed into this
* cell's constructor.
*
* #var array
*/
protected $_validCellOptions = [];
/**
* Default display method.
*
* #return void
*/
public function display()
{
}
}
secound display.ctp in Template\Cell\Carousel folder.
In CarouselCell.php display method add:
public function display()
{
$this->loadModel('Slides');
$slides = $this->Slides->find('all');
$this->set('slides', $slides->toArray());
}
in display.ctp
<?php foreach($slides as $slide) { ?>
YOUR CAROUSEL HTML HERE
<?php } ?>
On home.ctp
<?= $this->cell('Carousel');?>
Read more: http://book.cakephp.org/3.0/en/views/cells.html
// First you pass data from the controller:
public function home() {
$slider = array('img1.jpg','img2.jpg');
$this->set('slider', $slider);
}
app/View/Pages/home.ctp
<?php print_r ($slider); ?>
Related
Let's say I've got the following markup:
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Books, Literature and Languages
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Business and Consumer Information
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
<p>Some text in between</p>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Fine Arts and Music
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Genealogy
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
I want to wrap each group of .handorgel__* elements so that they are contained with a container <div class="handorgel">.
<div class="handorgel">
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Books, Literature and Languages
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Business and Consumer Information
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
</div>
<p>Some text in between</p>
<div class="handorgel">
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Fine Arts and Music
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>Hello</p>
</div>
</div>
<h3 class="handorgel__header">
<button class="handorgel__header__button">
Genealogy
</button>
</h3>
<div class="handorgel__content">
<div class="handorgel__content__inner">
<p>World</p>
</div>
</div>
</div>
There could be any number of elements within each group, and any number of groups of a page. How can I detect these groups and wrap them appropriately? I currently use DOMDocument for a number of things on this project, so if possible, I'd like to use that for this purpose as well, unless there's a clearly superior method.
Managed to get this working myself, after a bunch of trial-and-error. Wasn't quite as difficult as I assumed it would be, DOMDocument actually takes care of some of the removal logic itself.
/**
* Wrap handorgel groups in appropriate containers
*
* #param string $content
*
* #return string
*/
function gpl_wrap_handorgel_shortcodes(string $content): string {
if (! is_admin() && $content) {
$DOM = new DOMDocument();
// disable errors to get around HTML5 warnings...
libxml_use_internal_errors(true);
// load in content
$DOM->loadHTML(mb_convert_encoding("<html><body>{$content}</body></html>", "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);
// reset errors to get around HTML5 warnings...
libxml_clear_errors();
$body = $DOM->getElementsByTagName("body");
$handorgels = [];
$prev_class = "";
foreach ($body[0]->childNodes as $element) {
/**
* Ensure that only HTML nodes get checked/modified
*/
if ($element->nodeType == 1) {
$current_class = $element->getAttribute("class");
/**
* Find any handorgel elements
*/
if (preg_match("/handorgel__/", $current_class)) {
$group = array_key_last($handorgels);
/**
* If the previous class didn't include `handorgel__`, create a new handorgel object
*/
if (! preg_match("/handorgel__/", $prev_class)) {
$handorgels[] = [
"container" => $DOM->createElement("div"),
"elements" => [],
];
/**
* Update `$group` to match the new container
*/
$group = array_key_last($handorgels);
}
/**
* Append the current element to the group to be moved after all sequential handorgel
* elements are located for its group
*/
$handorgels[$group]["elements"][] = $element;
}
/**
* Update `$prev_class` to track where handorgel groups should begin and end
*/
$prev_class = $current_class;
}
}
/**
* Construct the grouped handorgels
*/
if ($handorgels) {
foreach ($handorgels as $group => $handorgel) {
$handorgel["container"]->setAttribute("class", "handorgel");
foreach ($handorgel["elements"] as $key => $element) {
/**
* Insert the container in the starting position for the group
*/
if ($key === 0) {
$element->parentNode->insertBefore($handorgels[$group]["container"], $element);
}
$handorgel["container"]->appendChild($element);
}
}
}
/**
* Remove unneeded tags (inserted for parsing reasons)
*/
$content = remove_extra_tags($DOM); // custom function that removes html/body and outputs the DOMDocument as a string
}
return $content;
}
add_filter("the_content", "gpl_wrap_handorgel_shortcodes", 30, 1);
I'm writing a CodeIgniter 3 application. My goal is to have a view, which is constantly (as the code goes along) flushed with the output content. I read some answers in stackoverflow, but I am not sure, how to do this.
I have a Controller wich renders the view, in the update method.
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class Dataupdate extends MY_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$this->render('dataupdate_list_view');
}
public function update() {
$this->render('dataupdate_update_view');
}
}
?>
Here is the class "MY_Controller".
<?php
defined('BASEPATH') OR exit('No direct script access allowed!');
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
$this->data['page_title'] = 'Team Portal';
$this->data['before_head'] = '';
$this->data['before_body'] = '';
}
/**
* Render method is used to render a view using a template.
* The given template delivers the HTML header and footer.
* The view contains the actual page content.
*
* #param string $the_view The view to be rendered
* #param string $template The template to render the view
*/
protected function render($the_view = NULL, $template = 'master') {
if ($template == 'json' || $this->input->is_ajax_request()) {
header('Content-Type: application/json');
echo json_encode($this->data);
} else {
// Get current user from database
$this->load->model('user_model');
$user = $this->user_model->get_record_by_name($_SERVER['REMOTE_USER']);
// Data to pass to view
$this->data['the_view_content'] = (is_null($the_view)) ? '' : $this->load->view($the_view, $this->data, TRUE);
$this->data['user'] = $user;
// Load view with data
$this->load->view('templates/' . $template . '_view', $this->data);
}
}
}
?>
Then I have a view, which outputs the content.
<div>
<!-- PAGE TITLE -->
<div class="page-title">
<h3>Daten Update</h3>
<div class="title_right">
</div>
</div>
<div class="clearfix"></div>
<!-- ALERTS -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Fehler<small>Kleiner Text</small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content bs-example-popovers">
<div class="alert alert-danger alert-dismissible fade in" role="alert">
<strong>Strong text.</strong>What to do now?
</div>
</div>
</div>
</div>
</div>
<!-- CONTENT -->
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Neues Daten Update ausführen</h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<?php echo form_open('dataupdate/update'); ?>
<input type="text" name="test" />
<button>Ausführen</button>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
This works fine. So far.
Now, what I try to achieve is the following:
I click on the "Ausführen" (Execute) button, which submits the form to the same url as this page
Then I want a php script to execute, which continously outputs content to the page. Not all content at once, but adds output after output to the page.
I hope I made myself clear.
Any suggestions or tutorials, on how to do that?
You'll need to force the output by calling ->_display method on the output class
For example for JSON output you'll do something like this:
$response = array('status' => 'OK');
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
Same thing goes when you load a view, if you're 100% sure you won't need anything else, You may call _display manually to get the output; just be sure to call exit after that otherwise you'll end-up with a duplicated output (one from your manual call & the other from the automatic call).
Have a look at the user's guide for output class: http://www.codeigniter.com/user_guide/libraries/output.html
I try to create an option page for a Wordpress theme. I manage to create the page, the Metaboxes , but when I send the form, the data are not saved. Before I show you my code , I specify that I use a class to generate the page more easily. Maybe this can guide you to the problem.
In this code I would like to save the "slide" option. This option is registred with register_setting() and visible in the slideshow_metabox() method that displays the form with fields that are named "slide [...]".
add_action('admin_menu', 'add_custom_admin_page');
function add_custom_admin_page(){
add_menu_page('CYBER Auto Pièces', 'CYBER Auto Pièces', 'manage_options', 'cyberautopieces', '' );
add_action( 'admin_init', 'cyberautopieces_register_settings' );
}
function cyberautopieces_register_settings() {
//register our settings
register_setting( 'cyberautopieces-settings', 'slide' );
}
new GeneratePageAdmin ('cyberautopieces','CYBER Auto Pièces','CYBER Auto Pièces', 'manage_options','cyberautopieces');
//Add some metaboxes to the page
add_action('add_meta_boxes','cyberautopieces_admin_metaboxes');
function cyberautopieces_admin_metaboxes(){
add_meta_box('slideshow_box','Slideshow','slideshow_metabox','toplevel_page_cyberautopieces','normal','high');
add_meta_box('save_box','Enregistrer','save_metabox','toplevel_page_cyberautopieces','side','high');
}
//Define the insides of the metabox
function slideshow_metabox(){
var_dump(get_option('slide')); // DISPLAY bool(false) :(
?>
<ul id="slideshow_box">
<li>
<a class="add-image-slider" href="#">
<div class="images half">
Choisir une image
<img src="" alt="">
<input type="hidden" name="slide[img][]">
</div>
</a>
<div class="text half">
<label><strong>Titre : </strong></label>
<input type="text" name="slide[title][]" value="" />
<label><strong>Slogan : </strong></label>
<input type="text" name="slide[slogan][]" value="" />
<strong>Situation : </strong>
<select id="slide_situation" name="slide[text_position][]">
<option value="left" selected>Gauche</option>
<option value="right">Droite</option>
</select>
</div>
<div class="add-delete">
<span class="add">+</span><span class="delete">-</span>
</div>
</li>
</ul>
<?php
}
//Define the insides of the metabox
function save_metabox(){
submit_button();
}
Here are the contents of my class GeneratePageAdmin :
class GeneratePageAdmin
{
var $hook;
var $title;
var $menu;
var $permissions;
var $slug;
var $page;
/**
* Constructor class for the Simple Admin Metabox
*#param $hook - (string) parent page hook
*#param $title - (string) the browser window title of the page
*#param $menu - (string) the page title as it appears in the menuk
*#param $permissions - (string) the capability a user requires to see the page
*#param $slug - (string) a slug identifier for this page
*#param $body_content_cb - (callback) (optional) a callback that prints to the page, above the metaboxes. See the tutorial for more details.
*/
function __construct($hook, $title, $menu, $permissions, $slug, $body_content_cb='__return_true'){
$this->hook = $hook;
$this->title = $title;
$this->menu = $menu;
$this->permissions = $permissions;
$this->slug = $slug;
$this->body_content_cb = $body_content_cb;
/* Add the page */
add_action('admin_menu', array($this,'add_page'));
}
/**
* Adds the custom page.
* Adds callbacks to the load-* and admin_footer-* hooks
*/
function add_page(){
/* Add the page */
$this->page = add_submenu_page($this->hook,$this->title, $this->menu, $this->permissions,$this->slug, array($this,'render_page'),10);
//var_dump($this->page);
/* Add callbacks for this screen only */
add_action('load-'.$this->page, array($this,'page_actions'),9);
add_action('admin_footer-'.$this->page,array($this,'footer_scripts'));
}
/**
* Prints the jQuery script to initiliase the metaboxes
* Called on admin_footer-*
*/
function footer_scripts(){
?>
<script> postboxes.add_postbox_toggles(pagenow);</script>
<?php
}
/*
* Actions to be taken prior to page loading. This is after headers have been set.
* call on load-$hook
* This calls the add_meta_boxes hooks, adds screen options and enqueues the postbox.js script.
*/
function page_actions(){
do_action('add_meta_boxes_'.$this->page, null);
do_action('add_meta_boxes', $this->page, null);
/* User can choose between 1 or 2 columns (default 2) */
add_screen_option('layout_columns', array('max' => 2, 'default' => 2) );
/* Enqueue WordPress' script for handling the metaboxes */
wp_enqueue_script('postbox');
}
/**
* Renders the page
*/
function render_page(){
?>
<div class="wrap" id="<?php echo $this->slug; ?>">
<?php screen_icon(); ?>
<h2> <?php echo esc_html($this->title);?> </h2>
<form method="post">
<?php settings_fields( $this->slug.'-settings' ); ?>
<?php do_settings_sections( $this->slug.'-settings' ); ?>
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' );
/* Used to save closed metaboxes and their order */
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
<div id="post-body-content">
<?php call_user_func($this->body_content_cb); ?>
</div>
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes('','side',null); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes('','normal',null); ?>
<?php do_meta_boxes('','advanced',null); ?>
</div>
</div> <!-- #post-body -->
</div> <!-- #poststuff -->
</form>
</div><!-- .wrap -->
<?php
}
}
Sorry for the long code , but I really do not know where the problem is.
Thanks you for your help ;-)
I'd say the problem lies within the way you're instantiating your class.
Use a hook to instantiate it like:
add_action( 'after_setup_theme', array( 'GeneratePageAdmin', 'init' ) );
Create the new method init of course, and do your work there.
If you're creating an options page, then the hook is likely an admin one, and not after_setup_theme, but you should get the idea.
What I would do is something like this:
public static function get_instance() {
// create an object
NULL === self::$instance and self::$instance = new self;
// return the object
return self::$instance;
}
Then, to instantiate it via Wordpress hook:
add_action( 'after_setup_theme', array( GeneratePageAdmin::get_instance(), 'init' ) );
Then, you should be able to use $this in your class
Finally, I've just had to add "action=options.php" in the header of the form and remove this section automatically handled by the settings_fields function.
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' ); ?>
For some pages of my application, I would like to use dvuhkolonchaty template. For this I would like to just use a different layout.
In the new layout except the variable $content, which displays the contents of a particular view, I would like to withdraw more other data in another column. Here is the code of the new layout:
<?php /* #var $this AdminController */ ?>
<?php $this->beginContent('/layouts/main'); ?>
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<!-- Any data -->
</div>
<div class="span8">
<?php echo $content; ?>
</div>
</div>
</div>
<?php $this->endContent(); ?>
And that's just where Any data, I would like to display a different not template information (eg, shape or edit the list of properties is constantly changing depending on the viewing record ID).
Q How can I print information in the Any data?
All your controllers will extends Controller class;
In the Controller class, you make a property like this:
public $anyData = null;
In your any actions, you can set `$this->anyData = "anything";
then in your layout, you can write :
<div class="span4">
<!-- Any data -->
<?php
if($this->anyData!=null) {
//Process $this->anyData here, echo or do something you like;
}
?>
</div>
I have a main view with a menu which helps me display another view. It's similar to this:
<div id="page">
<div id="menu">
Page1
Page2
</div>
<div id="content">
<!-- Page1 or Page2 are displayed here -->
</div>
</div>
I'm using php's Yii framework. Which makes me not to use <?php include("menuview.php"); ?>. So I'm looking for a different solution. I can do this with Ajax, but I would also like the link to change to mypage/controller/Page2. With Ajax I can only get it to this: mypage/controller/index#Page2
in main view, instead of include do
<?php echo $this->renderPartial('_page1', array('model'=>$model)); ?>
UPDATE:
protected/views/controller/page1.php and protected/views/controller/page2.php content at your liking
protected/views/layouts/custom.php:
<?php $this->beginContent('//layouts/main'); ?>
<div id="page">
<div id="menu">
<?php echo CHtml::ajaxLink('Page1', array('controller/page1'), array('update' => '#content')); ?>
<?php echo CHtml::ajaxLink('Page2', array('controller/page2'), array('update' => '#content')); ?>
</div>
<div id="content">
<?php echo $content; ?>
</div>
</div>
<?php $this->endContent(); ?>
protected/controllers/ControllerController.php:
class ControllerController extends Controller {
/**
* #var string the default layout for the views.
*/
public $layout = '//layouts/custom';
public function actionPage1() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page1');
else
$this->render('page1');
}
public function actionPage2() {
if (Yii::app()->request->isAjaxRequest)
$this->renderPartial('page2');
else
$this->render('page2');
}
}
UPDATE2:
If you need the link in address bar to change too then your only option is to use regular link and not ajax <?php echo CHtml::link('Page1', array('controller/page1')); ?>
using ajax the preferred way is using hash like you mentioned.