Displaying Content with laravel template without blade - php

I have this controller method
class Account_Controller extends Base_Controller
{
public $layout = 'layouts.default';
public function action_index($a,$b)
{
$data['a'] = $a;
$data['b'] = $b;
$this->layout->nest('content', 'test',$data);
}
}
And this is my layout
<div id = "content">
<?php echo Section::yield('content'); ?>
</div>
And this is my test.php
echo $a;
echo '<br>';
echo $b;
echo 'this is content';
When i access this
http://localhost/myproject/public/account/index/name/email
I get my layout loaded but test.php is not loaded. How can i load content in my template. I dont want to use blade.

When you nest a view within another it's content is defined as a simple variable. So, simply output it:
<?php echo $content ?>
Section is used when you need to change something on your layout (or any parent view really) from within the child view. For instance:
// on layout.php
<title><?php echo Section::yield('title') ?></title>
// on test.php
<?php Section::start('title'); ?>
My Incredible Test Page
<?php Section::stop(); ?>
<div class="test_page">
...
</div>

I think you need render for it, not sure, maybe partial loading:
<div class="content">
<?php echo render('content.test'); ?>
</div>
Look this sample for nesting views: http://laravel.com/docs/views#nesting-views
public function action_dostuff()
{
$view = View::make('controller.account');
// try dump var to grab view var_dump($view);
var_dump($view);
$view->test = 'some value';
return $view;
}
Or use instead blade: Templating in Laravel

Related

Having difficulty printing text from php function on html page

Here's an example of what I have going on:
common.php
<?php
class Common {
function test() {
echo 'asdf;
}
}?>
webpage.php
<?php
require_once("common.php");
?>
<html>
<body>
<?php test(); ?>
</body>
</html>
No matter what I've tried, I cannot get the function test to print any text to the page. With the actual webpage I was using, anything below the '' line didn't load with that portion included. I've been searching around for the past hour to figure this out, what am I doing wrong?
You have missing a ' closure also dont need a class for this, you should have just the function definition
<?php
function test() {
echo 'asdf';
}
?>
<?php
class Common {
function test() {
echo 'asdf'; // missing a ' closure added
}
}?>
You can access this function using a object of this class
<?php
require_once("common.php");
// instantiate the class before you use it.
$common = new Common(); // Common is a object of class Common
?>
<html>
<body>
<?php echo $common->test(); ?>
</body>
</html>
Alternatively, If you don't want to have a $common variable you can make the method static like this.
<?php
class Common {
static function test() {
echo 'asdf';
}
}?>
Then all you have to do to call the method is:
<html>
<body>
<?php echo Common::test(); ?>
</body>
</html>

set controller function as link

I have created simple mvc in php.
My controller is :
class front
{
function __construct()
{
}
function add_to_cart()
{
echo "123";exit;
}
}
Now, I want to pass id into this controller.
My view page is:
<span>ADD TO CART</span>
So, what link I have to write in href to get itemID in add_to_cart function?
I'm not sure about your question but this is simple example of using such functionality
Test.php
<?php
echo '<a href="../another_test.php/add_to_cart/?id=1231"/>Click Here</a>';
?>
another_test.php
<?php
class A{
function __construct(){
}
function add_to_cart(){
$id = $_GET['id'];
return $id;
}
}
$a = new A();
echo $a->add_to_cart();//1231
{BASE URL : http://localhost/projectname}/{indx.php(ifrequired)}/{CONTROLLER NAME : front}/{METHOD NAME : add_to_cart} / {VALUE : itemID}
In short {http://localhost/projectname}/{indx.php}/{front}/{add_to_cart}/{itemID}
function add_to_cart($itemID)
{
echo $itemID;exit;
}

object in Codeigniter

I began use RedbeanPHP and Codeigniter. In Codeigniter manual written, that if you transmit object from controller to view - properties of object become arrays.
I try get array ( object of redbeanphp ) in view from controller.
For example :
class Welcome extends CI_Controller {
public function index() {
$this->load->library('rb');
$post = R::dispense('post');
$post->title = 'HI';
$post->text = 'Hello World';
$post->count = 5;
$this->load->view('welcome_message',$post);
}
}
I don't understand, how I must appeal to variable of array ?
<p><?php echo $????['title'] ;?></p>
You could pass the variables for the templates as an array.
$this->load->view('welcome_message', array('something' => $post));
Then you can access it as $something in your template
title is your key- so it itself becomes a variable in the view.
just use it like a normal variable-
<p><?php echo $title ;?></p>

How can I pass variables to my views in php

I have a class which contains a method to load a view. This works perfect and the page is loaded correctly! The problem I'm having is that I can't seem to figure out a way to pass variables to the view that my method currently tries to load. How can I go by passing variables to the page? Is there a way I can manipulate my current code to make this work? Or am I going in the complete opposite direction?
Controller Class:
<?php
class Controller {
private $args = array();
public function view($page, $args = null) {
if ($args !== null) {
$this->args = $args;
extract($this->args);
}
$page = VIEWS . $page . '.php';
$template = $this->getTemplate($page);
echo $template;
}
private function getTemplate($file) {
ob_start();
include($file);
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
?>
Controller:
<?php
class Page extends Controller {
public function person($age) {
$data['age'] = $age;
$this->view('person', $data);
}
}
?>
View Contents:
</h1>My name is Dennis. My age is: <?php echo $age; ?></h1>
The end result is an undefined variable ($age).
The variables which you extract in view() are not available in getTemplate(). It is a different scope. You should instead extract them in the same method that does the rendering.
Also, what you have there is not an MVC view. It's just a template. A view in MVC is an instance which is responsible for UI logic. It should be requesting the information, that it needs, from model layer and then, if a response is not simply a HTTP location header, creating the HTML using multiple templates.
Try this:
</h1>My name is Dennis. My age is: <?php echo $data['age']; ?></h1>
The easiest way is to pass down the $args variable to getTemplate, and do the extract method in the getTemplate method
private function getTemplate($file, $args = null) {
if ($args !== null) {
extract($args);
}
ob_start();
include($file);
$template = ob_get_contents();
ob_end_clean();
return $template;
}
when you include and capture the the output, that php file is executed, so before inclusion all the variables must be present
If you dont want to pass down variables you can use $this->args inside of the template
You extract the variables in a different method than where you include the template. If you want to use plain variables in your templates, make sure that they are extracted in the same scope.

How to access "$this" from a function

I use an homemade MVC system, in which Views access the model by being within the context of a method, and therefore able to access $this.
Example of a view, included dynamically :
...
<div>
Hello <?= $this->user->name ?>
</div>
...
Now, I have some code that I would like to factorize into functions, with some extra parameters.
For example :
function colored_hello($color) {
?>
<div style="background-color:<?= $color ?>">
Hello <?= $this->user->name ?>
</div>
<?
}
The problem is that I do not have access to $this, since the function is not a method.
But I do not want to spoil my model or controller with presentation stuff.
Hance, I would like to be able to call this function dynamically, as a method.
Like aspect oriented programming :
# In the top view
magic_method_caller("colored_hello", $this, "blue")
Is it possible ?
Or do you see a better way of doing it ?
Take a look at Closure::bindTo
You'll have to define/call your functions slightly differently, but you will be able to access $this from inside your object.
class test {
private $property = 'hello!';
}
$obj = new test;
$closure = function() {
print $this->property;
};
$closure = $closure->bindTo($obj, 'test');
$closure();
Pass $this as a property, but in all seriousness: you shouldn't really have functions in your view files.
it is a bit a hack but you can use debug_backtrace() to get the callers object. But I think you can only public values:
function colored_hello($color) {
$tmp=debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
$last=array_pop($tmp);
$caller = $last['object'];
print_r($tmp);
print_r($last);
print_r($caller);
?>
<div style="background-color:<?= $color ?>">
Hello <?= $caller->user->name ?>
</div>
<?
}
(code is not testet but it gives you a hint :-) )
You could alternatively pass it into the function:
function coloured_hello($object, $color) {
//Code
$object->user->name;
}

Categories