How can I separate logic and presentation code when building a calendar? - php

Ok, we have a page. A very simple one. Let's call it page.php. It will have a controller and a view. The controller will just catch up some GET variables and print them out in the beginning of that page, the view will show up an header a footer and a paragraph that will contain the GET variables from the controller.
Now let's imagine we want to add a calendar. You think it's good and easy. Well, it seems not at all to me.
The calendar is made up of <table><tbody>X</tbody></table> which is pure HTML and a logic PHP script (that will fill the X) that will create (based on the time()stamp of the current date and time) the month dates and and numbers.
Now I encounter an ethic problem: How could I divide logic and view of that calendar?
The block of code that will create the dates will print <td> and <tr> elaborating the time(). But if the logic prints HTML isn't it bad? I think so, because you loose that comfortably think called "division of logic and view" that is typical of the MVC pattern.
At the same time if we take all the calendar logic to the controller and we just send to the view and array of dates (with coordinates X and Y to tell him the place where to put the first date of the month) the view will still need of elaborate the array and will became logic as well.
How could I really divide logic and view from this calendar?

Simply iterating over an array in a view is perfectly fine and not a violation of the MVC pattern. The logic happens in the controller where you populate the array. Even better would probably be to put the logic into a Model named Calendar.php and simply initialize it in the controller to pass it to the view.

u can put logic codes in a php file and assign the result to the variables that will show in view file (for example array)
in view file you can echo the variables and put html tags easily.

Related

how to implement shortcodes in pages or blog?

I am using php framework laravel and I would like to implement shortcodes.
Does anyone have a suggestion how to go about implementing shortcodes that could be inserted by user into a page or blog post.
Example, lets say I would like to have a shortcode for gallery that has some images, something like
[gallery=id]
which would then display that particular gallery. Ofcourse I have gallery model and gallery & images tables.
My first thought is that I would have to scan the content of the page/post and look for shortcode and when I find a particular shortcode, then what? What do I insert instead of it?
I can't insert php code that loops through the gallery and produces output. I guess the best way to do it would be to run a function that returns complete html code that I insert instead of the shortcode.
Is it possible to return a view from a function and insert the result into a page/post?
Depending on what kind of "shortcode" you want, there are numerous options here:
Blade Directives
Blade Layouts
Blade Sub-Views
Blade Stacks
To be clear, I'm assuming you want to insert this into content blocks stored in a db or something, not in your exiting view files.
Assuming you want inject the text into your view for display, you could allow content creators to use a templating format like mustache (https://en.wikipedia.org/wiki/Mustache_%28template_system%29) with server side callbacks/helpers/partials (in the view) to populate your own custom tags with dynamic content (gallery links in this case).
Main Points
I'd stick to a basic well known templating format like
mustache for this. In laravel, allow to Blade templating notation, it sounds like you want for this case to register a Blade::directive: https://www.laravel.com/docs/5.3/blade#service-injection.
I'd try to use an existing library if one such exists before
attempting to create your own short-tagging markup parsing /
callback paradigm. Blade has functions and methods handle such callbacks.
#2 is not necessary, but highly recommended, but #1 should be considered near religious.
UPDATE: as point out in other answers, Laravels built-in templating Blade, has this functionality, so stick to it's format and functionality.
Here's my suggestion (in pseudo-laravel/php):
Assume your marked up content from model pulled into the controller is in $content['blade_markedup_content' => 'here is my new gallery: #gallery(1)'] to be passed to the view.
Add a Blade directive #preprocess() to and in the template/view use <p>#preprocess($blade_markedup_content)</p> in place of just substituting raw <p>{{$blade_markedup_content}}</p>.
In the respective callback function for the #preprocess() directive
Instantiate a new $my_compiler = new BladeCompiler();
Register to this new blade compiler directives to handle the like #gallery(1) you put in your blade_markedup_content
return from the #preprocess() with $my_compiler->compileString($blade_markedup_content);
Viola... In theory, this should result in a parsed and substitute string from #preprocess($blade_markedup_content) in the original template.

How do I implement an Angular-like filter function in php?

I have a page that is full of tables (generated by a foreach loop) and every row displays, among other things, a month.
Now I want to give the user the ability to choose a month and have only the table rows for that current month displayed (or no table at all if that month is missing from the table). A bit like the filtering function in Angular JS.
I'm still a bit new to php and the only solution I can up with is to handle the whole thing somewhat clumsily with modifying CSS classes and having the superfluous rows hidden by CSS.
What would be a more efficient way to do this?
Your problem is related with the filter alternate of angular into php.
As you know php is server side. So, if you want to get the filter after a reload or by ajax method, you need a $_GET variable.
Use a different query string in your url for each click of each button. Then retrieve the GET variable and use it to get data from database for relevant category.

CakePHP: How to show the same query data in all my views, directly in the layout file

I'm creating a website with CakePHP; the problem now is that I want to show in the website footer (that will be visible on all the pages), the result of a query (e.g. "Most popular products").
What is the correct way with CakePHP to achieve this?
At the moment I created a mostPopularList() in my Product controller and a most-popular-list.ctp view that only outputs an <ul> list, thinking that then I could include the output of this file in my layout (default.ctp), but I didn't find the CakePHP way to do that.
Thanks!
Use a view cell or call the query in the AppControllers beforeRender() callback and set it there to the view. Taken from the documentation:
View cells are small mini-controllers that can invoke view logic and render out templates. They provide a light-weight modular replacement to requestAction(). The idea of cells is borrowed from cells in Ruby, where they fulfill a similar role and purpose.
View Cells
Controller::beforeRender()
If this is needed on really every single page I would probably go for the beforeRender() callback, easy to do and change globally.

Where to format numbers in CodeIgniter?

Should I format my numbers 1234.20 => 1'234.20 in model or in the view using php and codeigniter?
It belongs to the view layer.
The model only cares about the value, not how you present it to the user. Also, you may need to display it differently, if your site is multilingual.
In the view, since the transformation is presentational in nature.
I would format it in the view, because this sperates the visual from the rest.

CakePHP: is it possible to use model data in layouts?

I would like to display a ticker on each page what scrolls some of the latest datas in it. I think to put in the layout file would be more simply if I can read the model data from the layout.
Is it possible?
Or does layout code has access to the variables generated by a controller and passed to a view?
Or is it the only way to make an element and place it inside each view?
Pretty hard to understand what you're asking but i think it's something along the lines of
"How do I make a variable or dataset available to all views globally in CakePHP"
If so it's probabaly worth looking at the app_controller.php file (book.cakephp.org/view/829/The-App-Controller) and the method startUp() you could run a query and assign it using $this->set('ticker', $sql_result);
Perhaps what you need is an element.
And to answer the question : Yes, layout files have access to the variables set to the view in the controller, just like the views and the elements does.

Categories