Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 306869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:30:24+00:00 2026-05-12T07:30:24+00:00

Given a Category (parent) and Product (child) tables in a database, say, I want

  • 0

Given a Category (parent) and Product (child) tables in a database, say, I want to create Smarty Block Plugins that would enable template snippets similar to this:

{products category="Some Category"}
    <h1>{products_name}</h2>
    <p>{products_description}</p>
{/products}

I believe plugins like these would help avoid repeated chunks of code that read a database and do a smarty-assign on the result in my controller.

I know how to write this as a smarty function. But I am looking for a block version to give the template designer the flexibility to style the individual columns in whatever way he wants. I am a long-time Perl programmer and new to Smarty. Perl users will recognise something like this in the Movable Type Templating System for example, and I wonder if a smarty version is possible.

Is something like this possible in Smarty at all? Is it a good thing to make a DB call from inside a smarty plugin?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:30:24+00:00Added an answer on May 12, 2026 at 7:30 am

    My suggestion is use configuration array ($conf) with the SQL query template to use inside the plugin for simple modification.
    Of course is not a good thing make a DB call inside the Smarty plugin. Instead, you can load the results in the $conf array making the DB call in the PHP script and unload in the plugin, as you wish.

    This is the Smarty plugin:

    <?php
    function smarty_block_products($params, $content, &$smarty, &$repeat)
    {
        global $conf;
    
        $category = $params['category'];
        $md5 = md5($category);
        if (empty($content))
        {
            if (empty($category))
            {
                $smarty->trigger_error("products: missing 'category' parameter"); 
            }
            $sql = str_replace('{$category}', $category, $conf['get-products-sql-template']);
            $query = mysql_query($sql);
    
            $result = array();
            while ($row = mysql_fetch_assoc($query))
            {
                $result[] = $row;
            }
            if (count($result) == 0)
            {
                $result = false;
            }
            $GLOBALS['__SMARTY_PRODUCTS'][$md5] = $result;
        }
        if (is_array($GLOBALS['__SMARTY_PRODUCTS'][$md5]))
        {
            $field = "product";
            if (isset($params['item']))
            {
                $field = $params['item'];
            }
    
            $product = array_shift($GLOBALS['__SMARTY_PRODUCTS'][$md5]);            
    
            $smarty->assign($field, $product);
    
            if (count($GLOBALS['__SMARTY_PRODUCTS'][$md5]) == 0)
            {
                $GLOBALS['__SMARTY_PRODUCTS'][$md5] = false;
            }
            $repeat = true;
        } else {
            $repeat = false;
        }
        echo $content;
    }
    ?>
    

    the Smarty template:

    {products category="Some Category" item=product}
        <h1>{$product.name}</h2>
        <p>{$product.description}</p>
    {/products}
    

    and the PHP:

    <?php
    require 'Smarty/Smarty.class.php';
    
    $smarty = new Smarty;
    
    $conf['get-products-sql-template'] = 'SELECT product.* FROM product INNER JOIN category ON category.id = product.category_id WHERE category.title = \'{$category}\'';
    
    $smarty->display('test.tpl');
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We want to fetch parent child in such a way that it gives me
I want to create a Category class that may or may not have a
Given a set of questions that have linked survey and category id: > db.questions.find().toArray();
Given m2m relation: items-categories I have three tables: items , categories and items_categories that
Domain: class Category string Name class Product IDictionary<string, Product> Parents Tables: Categories (ID, Name)
I'm working on a database design that allows for a product to be in
I'm trying to get a parent category for a given sub category using the
I have a Standard table, which sotres parent, child category relationship...like this. id, parent,
Given class Category(db.Model): name = db.Stringproperty() Say I have a nested hierarchy -root |-a
It loads category but always with null parent. I want get hierarchy category with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.