• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with parser

#1
Hi guys,
I have a problem that I can't solve right now, I'm using the parser library, and i need it to show a defined variable stored in the database in the view.

PHP Code:
$data = [
  
'site_name' => $ConfigurationSite->name,
 
 'seo_title' => $ConfigurationSite->title,
  
'seo_book_title' => $SeoBooks->title
]; 




I have this stored in the database: "Buy on {site_name}" that I save it to seo_book_title

View: 
Code:
<meta property="og:description" content="{seo_book_title}" />


And the result is:  "Buy on {site_name}" when the name defined above should appear on site_name.

Sorry about my English, I'm Spanish.
I hope you can help me, thank you and a greeting.
Reply

#2
Haven't used it myself, but over here https://www.codeigniter.com/userguide3/l...sage-notes looks like you need to set seo_book_title first in array, then follow-ups that might be in the first string after that.

PHP Code:
$data = [
   
'seo_book_title' => $SeoBooks->title,
   
'site_name' => $ConfigurationSite->name,
   
'seo_title' => $ConfigurationSite->title
]; 
Reply

#3
(08-31-2018, 08:57 AM)Pertti Wrote: Haven't used it myself, but over here https://www.codeigniter.com/userguide3/l...sage-notes looks like you need to set seo_book_title first in array, then follow-ups that might be in the first string after that.

PHP Code:
$data = [
 
  'seo_book_title' => $SeoBooks->title,
 
  'site_name' => $ConfigurationSite->name,
 
  'seo_title' => $ConfigurationSite->title
]; 

No, it doesn't work, I don't know if I've been able to explain myself properly.
I have this stored in my database: "Buy on {site_name}" and I want you to show me on the new label(seo_book_title) what I have in the database, It should work, since site_name is already defined, but it shows text as I have it in the database.
Reply

#4
You need to use the template parser, and not the normal views.

$this->load('parser');
$this->parser->parse(YOUR_TEMPLATE_FILENAME,$data);
Reply

#5
(08-31-2018, 10:51 AM)ciadmin Wrote: You need to use the template parser, and not the normal views.

$this->load('parser');
$this->parser->parse(YOUR_TEMPLATE_FILENAME,$data);

yes, that's what I'm doing, The problem is that in my database I save {site_name}, to show it with more text with {seo_title_books} and {site_name} it shows it as it is.
Reply

#6
You're going to have to show a bit more code, namely where you retrieve the data, before building the $data array, and perhaps the view fragment that isn't working properly.
Reply

#7
Try this:
PHP Code:
// ADDED quotes because variables not available
$data = [
 
 'site_name'      => '$ConfigurationSite->name',
 
 'seo_title'      => '$ConfigurationSite->title',
 
 'seo_book_title' => '$SeoBooks->title'
]; 

// http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

// BEWARE OF TRAILING SPACES
$tmp = <<< ____TMP
<meta property="og:description" content="{$data['seo_book_title']}" />

____TMP;
// BEWARE OF TRAILING SPACES

echo htmlspecialchars($tmp); 

Result: 

<meta property="og:description" content="$SeoBooks->title" />
download and make your system directory strict
Reply

#8
Nothing, no one seems to understand.

I define the variables to be used in the view with parser:
PHP Code:
$ConfigurationSite $this->MainModel->Configuration();
$SeoBooks $this->Books->bookSeo();

$data = [
 
   'seo_book_title' => '$SeoBooks->title',
 
   'site_name' => $ConfigurationSite->name,
 
   'seo_title' => $ConfigurationSite->title,
 
   'seo_description' => $ConfigurationSite->description,
 
   'site_logo' => $ConfigurationSite->logo,
 
   'site_url' => base_url(),
 
   'site_categories' => $siteCategories
];

$this->parser->parse('template/default/libreria_head'$data); 

Model:

PHP Code:
public function Configuration()
{
 
   $this->db->cache_on();
 
   $sql  $this->db
        
->select('*')
 
       ->from('configuration')
 
       ->get()
 
       ->row();
 
   $this->db->cache_off();

 
   return $sql;
}

public function 
bookSeo()
 
   {
 
       return
            $this
->db
                
->select('title')
 
               ->from('books_seo')
 
               ->where('id_book',0)
 
               ->get()
 
               ->row();
 
   }

#return this value: "Buy On {site_name}" 

View:

PHP Code:
<html>
 
   <head>
 
       <meta charset="utf-8">
 
       <meta name="viewport" content="width=device-width, initial-scale=1">
 
       <title>{seo_title}</title>
 
       <meta name="description" content="{seo_description}"/>
 
       <meta name="robots" content="noodp" />
 
       <link rel="canonical" href="{site_url}" />
 
       <meta property="og:locale" content="es_ES" />
 
       <meta property="og:type" content="website" />
 
       <meta property="og:title" content="{seo_title}" />
        <
meta name="og:description" content="{seo_description}" />
 
       <meta property="og:url" content="{site_url}" />
 
       <meta property="og:site_name" content="{site_name}" />
 
    


In my database I have this string: Buy on {site_name}, If {site_name} was already defined above in $data, because when I show {seo_book_title}. 
{Site_name} is shown like this, and not with the name that was defined?
Reply

#9
The template parser is not recursive. You will have to use it once to substitute for site_name, and then again for tha main template, for instance something like...

Code:
$data = ...;
$data['seo_book_title'] = $this->parser->parse($SeoBooks->title,$data,true);
$this->parser->parse(('template/default/libreria_head', $data);
Reply

#10
(09-01-2018, 07:44 AM)ciadmin Wrote: The template parser is not recursive. You will have to use it once to substitute for site_name, and then again for tha main template, for instance something like...

Code:
$data = ...;
$data['seo_book_title'] = $this->parser->parse($SeoBooks->title,$data,true);
$this->parser->parse(('template/default/libreria_head', $data);

It's still not working. I don't know how to get out of this, I'm thinking of having 2 different views and directly pass the values of parse hay, so as not to have to save anything in the database, Since I think it'll be easier and this is taking up a lot of my time.

Thanks to all for the help.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.