phpkirby

About template and object problems with Kirby CMS


I met a few difficult problems when trying to write an API for my blog built with kirby cms .

In a template file called "article.php" in kirby,I want to put the values (e.g. "$page->title()") into an array in order to use the function "json_encode()" to render a json result and then output.

The usage of the kirby method (e.g."$page->title()") is as simple as below:

echo $page->title();

And it can return the title of the current page (e.g."titleOfThisPage").I think(may be wrong,for I am a noob) the value can also be passed to an array in the way like this:

$title = array(
"name" => "title",
"value" => $page->title()
);

But unfortunately,it doesn't work correctly.

Could you please tell (or teach) me what is wrong,and how can I achieve the aim?

Here enclosed the original code:

if($_REQUEST['get'] == "id")
  {
    $json_array = array(
    "status" => "success", 
    "id" => $page->id(), 
   );
   $json_output = json_encode($json_array);
   echo $json_output;
   exit;
  }
  elseif($_REQUEST['get'] == "title")
  {
    $json_array = array( 
    "status" => "success",
    "title" => $page->title(),
   );
   $json_output = json_encode($json_array);
   echo $json_output;
   exit;
  }

This is my first post.I am a Chinese high school student but also a blog and code lover.I find it hard to ask such questions in some websites in China,so I come to Stack Overflow.Please help me with any mistakes(not only programing errors,but also English grammar mistakes for I am also studying English at school) in any of my post, as a noob I would obviously appreciate it!Hoping for an easy solution coming out soon and a long live in Stack Overflow.


Solution

  • Have found the way out,just adding the type is OK:

      elseif($_REQUEST['get'] == "title")  
      {
        $json_array = array(
         "status" => "success",
         "title" => (string)$page->title(),
       );
       $json_output = json_encode($json_array);
       echo $json_output;
       exit;
      }