|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
lovesmarty Smarty n00b
Joined: 16 Mar 2016 Posts: 3
|
Posted: Wed Mar 16, 2016 5:29 am Post subject: application logic and template presentation |
|
|
hi all, i have a question about the logic and presentation
in my php script is save a whole set info in some array, for example:
Code: |
<?php
// many set array inside
function get_data_array($name) {
switch($name)
{
case 'site_items':
$arr = array(
'book' => 'Title book',
'car' => 'Title car',
'...' => 'Title ...',
'...' => 'Title ...',
);
break;
case 'book_child':
$arr = array(
0 => array('sql_count_total' => 'WHERE book_type = 0', 'display_name' => 'HTML book', 'icon' => 'b1.gif', 'css' => 'style_b1'),
1 => array('sql_count_total' => 'WHERE book_type = 1', 'display_name' => 'CSS book', 'icon' => 'b2.gif', 'css' => 'style_b2'),
2 => array('sql_count_total' => 'WHERE book_type = 2', 'display_name' => 'PHP book', 'icon' => 'b3.gif', 'css' => 'style_b3'),
);
break;
case 'car_child':
$arr = array(
0 => array('sql_count_total' => 'WHERE car_type = 0', 'display_name' => 'Car A', 'icon' => 'c1.gif', 'css' => 'style_c1'),
1 => array('sql_count_total' => 'WHERE car_type = 1', 'display_name' => 'Car B', 'icon' => 'c2.gif', 'css' => 'style_c2'),
2 => array('sql_count_total' => 'WHERE car_type = 2', 'display_name' => 'Car C', 'icon' => 'c3.gif', 'css' => 'style_c3'),
);
break;
default:
die("error_msg", __FILE__, __LINE__);
}
return $arr;
}
?>
|
Code: |
<?php
// book.php
$doc_id = 'book';
$navbar_arr = get_data_array('site_items');
$doc_arr = get_data_array($doc_id . '_child');
// other action below ...
$book_id = array_key_exists($_GET['book_id'], $doc_arr) ? $_GET['book_id'] : 0;
?>
|
Code: |
<?php
// car.php
$doc_id = 'car';
$navbar_arr = get_data_array('site_items');
$doc_arr = get_data_array($doc_id . '_child');
// other action below ...
$car_id = array_key_exists($_GET['car_id'], $doc_arr) ? $_GET['car_id'] : 0;
?>
|
the key points is $doc_id
just need to loop any array return by get_data_array() to gen navbar, menu, menu_item_count ...etc
$doc_id = 'book' will get all the info used on 'book' such as (display_name, icon, css ...etc)
what sql stmt send to database
and also affect to validate $_GET , $_POST
so that means $doc_id and get_data_array() is control many things
but the concept of Smarty is split logic and presentation
so need to remove the display name, icon, css into tpl, this means need to create an array in tpl too
and make sure the php and tpl both array is same index and order
my problem is how to split logic and presentation in my case |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Mar 16, 2016 10:26 am Post subject: |
|
|
In your case, you just rewrite your abomination from scratch.
This is unreadable, unbearable and just plain wrong.
If you want database, use database. SQLite, of all things, is supported on all platforms PHP is run on, and does not require additional servers. If your load is 99,9% read and 0.0001% write, it will serve you just fine. |
|
Back to top |
|
lovesmarty Smarty n00b
Joined: 16 Mar 2016 Posts: 3
|
Posted: Wed Mar 16, 2016 5:15 pm Post subject: |
|
|
thx for your reply, just do some google search, find something about MVC,
but not really understand about it, anyways thx again |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Wed Mar 16, 2016 10:13 pm Post subject: |
|
|
MVC does not map to web as much as people hoped so.
As a pattern, it is more suitable for desktop application programming, when all your work is done in a single place.
On the web, you have two dintinct places that participate equally: a server, processing requests, building and sending out replies, and a client, requesting data and interacting with user. When user does something to the web page, it is not your controller responds to their actions, it is their browser, and you don't know about action until a browser decide to tell you. You can't even know, if browser told you about exactly what user did, strictly speaking.
Anyway… I'm rambling.
Answering your question more directly, I suggest you take the time to study Smarty documentation and google some example articles on the subject, to understand capabilities of the engine, and how you can apply them to your code.
I'm not going to lie to you, what you've shown is ugly and will need a heavy refactoring to work clearly and allow for transparent extension.
But it is also a good test for basic understanding of Smarty. Do it, and you'll feel much better, once you grasped the basics. |
|
Back to top |
|
lovesmarty Smarty n00b
Joined: 16 Mar 2016 Posts: 3
|
Posted: Thu Mar 17, 2016 3:59 pm Post subject: |
|
|
oh after i google "PHP MVC"
showing many resources about it, and some new name i never know called:
Laravel
CodeIgniter
CakePHP
seems MVC not only for desktop application programming
after a few hours to read some Model-View-Controller doc, what i learn is
http://localhost/test.php?act=list_all_data
now Controller decide the action = list_all_data
and talk to Model, i want the data of "list_all_data"
now Model say: ok i connect database get all the data, and send back the data to Controller
and now Controller send the data to View(in this case is Smarty) do the html output
and some rules:
(Don’t do these):
View:
1. NOT ALLOW connect DB
Controller:
1. NOT ALLOW include HTML
2. NOT ALLOW connect DB
Model:
1. NOT ALLOW output any HTML
(DO these):
View:
1. is modular - program fragment
2. contains loop (loops), and simple logic if
Controller:
1. Erase all view And database information
2. Provide all view Information needed
3. Apply business rules for data (business rules)
4. Call Database models To access information (store/retrieve data)
5. Handle all error messages
Model:
1. Apply limited commercial logic (business logic) -- If any - to the library function
2. Capture any errors and send it to controller
3. Perform minimal data integrity checking
if changed DB, no need to re-write View and Controller
ofc if changed View, no need to re-write Controller and Model
hmmm i think i better to learn the basic concept first and try install CodeIgniter in VMware to take a look what is it
anyways seems this is off-topic in the Smarty forum |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|