Welcome Guest, Not a member yet? Register   Sign In
How can I pass a variable from a Twig view to a controller?

(This post was last modified: 11-27-2020, 01:48 AM by Ajax30.)

I am working on a online newspaper/blogging application with CodeIgniter 3.1.8 and Bootstrap 4. I have decided to add themes to it. The application is not HMVC, only MVC.

I thought it was a good idea to use the Twig template engine to the theme(s). For this purpose, I use CodeIgniter Simple and Secure Twig.

In the application's frontend, I have turned classic Codeigniter views into Twig views. This created a problem with the form used to add comments.

Since I replaced

<?php echo form_open (base_url('comments/create/') . $post->id, array('id' => 'commentForm', 'class' => 'comment-form ajax-form', 'data-post'  => 'comment')); ?>
   <input type="hidden" name="postid" value="<?php echo $post->id; ?>">
   <input type="hidden" name="slug" value="<?php echo $post->slug; ?>">

   <div class="form-group <?php if(form_error('name')) echo 'has-error';?>">
     <input type="text" name="name" id="name" class="form-control" placeholder="Name" data-rule-required="true">
     <?php if(form_error('name')) echo form_error('name'); ?>

   <div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
     <input type="email" name="email" id="email" class="form-control" placeholder="Email" data-rule-required="true">
     <?php if(form_error('email')) echo form_error('email'); ?>

   <div class="form-group <?php if(form_error('message')) echo 'has-error';?>">
     <textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Comment" data-rule-required="true"></textarea>
     <?php if(form_error('message')) echo form_error('message'); ?>

   <div class="form-group">
     <input type="submit" value="Comment" class="btn btn-block btn-md btn-success">
<?php echo form_close(); ?>


<form id="commentForm" action="{{base_url}}comments/create/{{post.id}}" method="post">
    <input type="hidden" name="postid" value="{{post.id}}">
    <input type="hidden" name="slug" value="{{post.slug}}">
    <div class="row uniform">
        <div class="form-controll 6u 12u$(xsmall)">
            <input type="text" name="name" id="name" placeholder="Name" data-rule-required="true" />
        <div class="form-controll 6u$ 12u$(xsmall)">
            <input type="email" name="email" id="email" placeholder="Email" data-rule-required="true" />
        <div class="form-controll 12u$">
            <textarea name="comment" rows="6" id="message" data-rule-required="true"></textarea>
        <div class="12u$">
            <input type="submit" value="Add comment" class="button special fit" />
create method inside the Comments controller fails, as there is no longer a value for the variable.

I use the slug to identify the post, because that's what I have in the url. I get the post's id from the form (the hidden input).

public function create($post_slug){
    $post_id = $this->input->post('postid'); // this has become useless
    $post_slug = $this->input->post('slug');
    $data = $this->Static_model->get_static_data();
    $data['base_url'] = base_url("/");
    $data['pages'] = $this->Pages_model->get_pages();
    $data['categories'] = $this->Categories_model->get_categories();
    $data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=0);
    $data['post'] = (object) $this->Posts_model->get_post($post_slug);
    $data['comments'] = $this->Comments_model->get_comments($post_id);
    $data['tagline'] = "Add comment";

    $this->form_validation->set_rules('name', 'Name', 'required');
    $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
    $this->form_validation->set_rules('message', 'Comment', 'required');
    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    $this->twig->addGlobal('maincss', base_url('themes/caminar/assets/css/main.css'));

    if($this->form_validation->run() === FALSE) {
        $this->twig->display('themes/caminar/layout', $data);
    } else {

The posting of the form data is done via jQuery Ajax:

    rules: {
      email: {
        email: true

    submitHandler: function(form) {
      var form = $("#commentForm"),
      $fields = form.find('input[type="text"],input[type="email"],textarea'),
      url = form.attr('action'),
      data = form.serialize();
        type: "POST",
        url: url,
        data: data,
        success: function() {
          $('#comment_add_msg').text("Your comment will be published after approval")
        error: function() {
          .text("Sorry, we could not add your comment")

How can I pass the post's id from comments.twig to the Comments controller?

First, where is your Comments controller?

Post the code also, so it can be easy to determine how you are getting the $post variable.
Check out my blog: https://lagbaja.dev

(11-27-2020, 04:08 AM)sammyskills Wrote: First, where is your Comments controller?

Post the code also, so it can be easy to determine how you are getting the $post variable.

It is that create($post_slug) method.

Theme © iAndrew 2016 - Forum software by © MyBB