Right now I have something like this in my mustache template:
{{#render}}{{widget}}{{/render}}
And I have a viewModel which contains this code:
public function render() {
$View = $this->_View;
return function($widgetName, Mustache_LambdaHelper $helper) use (&$View) {
$widget = $helper->render($widgetName);
return $helper->render($View->mustache->getPartialsLoader()->load("$widget.view"));
};
}
I think you can see what I am trying to do here. I am trying to render a partial who's name is contained inside the widget-key of the current context.
Now the thing is, I really don't like the format I'm using in the template. I would prefer if I could write something like this:
{{renderWidget}}
then I would need to be able to access the current context directly in some way.
public function renderWidget() {
return function($context) {
return $helper->render($View->mustache->getPartialsLoader()->load("$context[widget].view"));
}
}
Can anybody tell me if that is possible in one way or another?
Related
In my MVC view files, there exist strings I may have a translation for. In a file with access to the database (the model), I can do:
$Lang->say('Welcome');
Here is what it's doing:
public function say($string) {
if (empty(self::$vocabulary)) {
self::$vocabulary = $this->loadLanguage($this->currentLanguageID()); // Load vocabulary for current language
}
if (isset(self::$vocabulary[$string])) {
return self::$vocabulary[$string];
}
return $string;
}
I need access to this say() function from within my view. Short of passing the entire vocabulary array to the view, how would I do this?
What you need to do is require_once your .php file that contains your class. You can then instantiate your class into a object like $Lang and call $Lang->Say() from your view.
For example:
require_once "file_that_holds_class.php";
$Lang = new classNameHere();
$result = $obj->Say("whatever_string_value");
echo $result;
Now you can do whatever it is you need to do with the string.
I want to pass variables to some.phtml from zf2 view helper.
I want to return some data inside my view/application/some/some.phtml
public function __invoke($request)
{
}
can anyone suggest?
Why not just do:
public function __invoke($request)
{
$myVar = 'these';
$myOtherVar = 'probably';
$anotherVar = 'arent really strings';
return [$myVar, $myOtherVar, $anotherVar];
}
But I would probably try to refactor so this is all done in the controller. But if you state the actual problem you are having and what you are trying to achieve you will probably get a better answer
I want to know the right way to handle this scenario: modify a variable within a method with a service. I know you could just do:
$input = $this->modify($input)
But that doesn't feel like the correct OOP way of handling the situation. Please see the inline comments.
class myClass {
public function __construct(Service $service)
{
$this->service = $service;
}
public function work($input)
{
// $input = SOMETHING
$this->service->modify($input);
// $input = SOMETHING MODIFIED
Entity::create($input);
}
}
It can be achieved by passing in the input as reference:
public function modify(&$input) {
// ^ See what I did there?
A better approach would be to return the modified input, and assign it again:
$input = $this->service->modify($input);
Don't start messing with references until you know exactly what you're doing.
I have two functions loadView() and render() in my view class.
public function loadView($view){
if(file_exists(APP.'view/'.$view)){
require(APP.'view/'.$view);
}
else{
die(APP.'view/'.$view.' not found.');
}
}
public function render($view,$data = array()){
if(!empty($data)){
extract($data);
ob_start();
//$this->loadView($view); -------------- not woriking
require(APP.'view/'.$view); ------ working
$this->output = ob_get_clean();
echo $this->output;
}
}
Whenever I call the loadview function from the render its not working. But if I include the view file directly after extracting data, it works. Anybody can tell me Why is this happening here or any solution ?
This will not work, because require() includes the required code in the place of the require() statement and in the scope of that location. In this case, the scope is the loadView() method. Within that method scope the extracted variables from the render() method are not available.
Instead of finding a solution for that, I would encourage you to use a template engine for your views like Twig. This is standard practice in MVC frameworks. Then, you could do something like this:
public function render($view,$data = array()){
$template = $twig->loadTemplate($view);
echo $template->render($data);
}
I am working on a backend project. I need to return a static object withing another static object:
Class this_is_a_very_long_class_name
{
public static function call()
{
return self;
}
public static function script_link($link)
{
//doing stuff here...
}
}
Class Main
{
public static function view()
{
// trying to return View object
return this_is_a_very_long_class_name::call();
}
}
and I am trying to use it like this:
Main::view()::script_link('Some script');
So how can I accomplish that?
P.S.: I am not looking for another solution. I am looking for a answer what I asked.
You don't need that.
Use
View::script_link();
Also this is wrong and misleading view()->script_link because script_link is static
Addendum
If you your problem is your class name length I suggest you to create simple wrapper for this.
function createLink($string){
return VERY_LONG_CLASS_NAME_HELLO_PHP_NAMESPACE::script_link($string);
}
this way you just need to createLink();
in php 5.3: return new View(); (instead of return View::self;).
Manual: http://php.net/manual/en/language.oop5.basic.php#example-159
in php 5.2 use ReflectionClass
I think your syntax on the call is wrong. Since it is static, what you are trying to do would look something like this:
Main::view()::script_link('Some script');
Except that would give you a syntax error. Also, since it is static, you don't need to return anything. You should make it two separate calls:
Main::view();
View::script_link("Some script");
It makes no sense to say "I need to return a static object". If the class is defined, then the static object is present and can be accessed.
You just need a variable to hold the class, as a direct call is invalid syntax
Sample:
Class Main
{
public static function view($type)
{
// return some class
switch ($type) {
case "View 2":
return View2;
break;
default:
return View;
}
}
}
$v = Main::view("normal view");
$v::script_link('test');
Are you looking for functionality as late static binding? Which is supported from PHP 5.3. See here: http://php.net/manual/en/language.oop5.late-static-bindings.php