Imagine you have the following scenario:
In you application, you have several models that are "Commentable". Comments are shown in
an identical manner and can thus utilize the same template. In the default view of the
comments widget, the two most recent comments are shown, with a link to load remaining X
comments.
The widget also have a form (i.e textarea that submits on enter-key) for adding a new
comment to the model.
Given the above requirement, where is a reasonable place to generate the links that the form and load-link needs to do its work?
Should the links be generated in the view that calls the template, together with the Commentable model? i.e.
<?php
echo $this->partial('path/to/template/comments.phtml', array (
'add-link' => $this->url($params, $routeName),
'load-link' => $this->url($params, $routeName),
'comments' => $this->model->getComments()
);
Is it, at all, acceptable to ask the Commentable for these links? I.e in the comments.phtml-template:
<div class="comments">
<div class="loadLink">
<a href="<?php echo $this->comments->getLoadLink() ?>">
<?php echo sprintf('Show all %d comments', count($this->comments)); ?>
</a>
</div>
<div class="container">
<?php
foreach ($this->comments->getFirst(2) as $comment) {
echo $this->partial('comment.phtml', array('comment' => $comment);
}
?>
</div>
<div class="addComment">
<form action="<?php echo $this->comments->getAddLink() ?>" method="post">
<div>
<textarea class="autogrow" name="comment"
placeholder="Write a new comment" rows="1" cols="80">
</textarea>
</div>
</form>
</div>
</div>
Since MVC advocates that the view can communicate with controllers, is route generation viewed as a way to "communicate" with a controller, even if it doesn't do so in an object-to-object manner?
In an MVC application you should never ask the model to construct a link. You should add some view helper that may use the model to generate these links.
Related
Do get the question clear, to make it clear let me elaborate; what i need to achieve is that i have a page where pagination is going forward -working and when ever i paginate the page, page goes to the top of the page since it did refresh but i need it to stay at the div where pagination links are at. The content area where the pagination content is so, that it may appear that page did not load only content was updated.
Do get the point.
code as follows:
<div class="large-12 columns" id="paginate" style="border-right: 1px solid #E3E5E8; height: 572px;">
<article>
<hr><div class="row">
<div class="large-6 columns">
<?php
//stories complete do not edit.
foreach($stories as $story){
echo '
<p>'.$this->Html->image($story['image'].'.jpg', ['alt'=>'image for article']).'</p>
</div>
<div class="large-6 columns">
<h5>'.$story['title'].'</h5>
<p>
<span><i class="fi-torso"> Creator: '.$story['creator'].' </i></span>
<span><i class="fi-calendar"> '.$story['created'].' </i></span>
</p>
<p>'.$story['story'].'</p>';
//same place after pagination
//
}
?>
</div>
<div style="padding: 2px;float: left;margin-top: 31%;margin-left: -1206px;">
<ul class="pagination" role="navigation" aria-label="Pagination">
<li><?php echo $this->Paginator->prev(' < ' . __(''));?></li>
<li><?php echo $this->Paginator->numbers();?></li>
<li><?php echo $this->Paginator->next(' > ' . __(''));?></li>
</ul>
</div>
</div><hr>
</article>
</div>
According to the code and earlier Cakephp code we were able to add a link to div with paginator and it would take us direct to a specific div, i can't figure out syntax to do that with cakephp-4.x so be kind and give your best solution with least amount of code.
I have found a solution to problem. That is far constructive than any other solution one can find anywhere from internet. I will describe code and put code snippet so that everyone and anyone is able to do so with just a read from my code snippet.
First Declare Pagination for a specific Model. After that Initialise Paginator code snippet. Once you have done above mention so, load and Query paginator to given model.
After that Set variable along with compact.
At template side make sure you have followed above mention process all your given code will be assigned at controller end and works at template side automatically. I put that code for template at bottom of snippet take a good read and implement your code where ever and when ever there is request and response among controller and template.
Put below code to controller:
public $paginate = [
'Stories' => ['scope' => 'story',
'limit' => 1,
]
];
public function initialize(): void
{
parent::initialize();
$this->loadComponent('Paginator');
}
public function index()
{
// Paginate property
$this->loadComponent('Paginator');
$stories = $this->paginate($this->Stories->find('all', ['scope' => 'story']));
$this->set(compact('employers'));
}
Below code to Template:
<div class="column" id="success_story">
<hr>
<h4 style="margin: 0;" class="text-center">SUCCESS STORIES</h4>
<hr>
</div>
<?php
//stories complete do not edit.
foreach($stories as $story){
echo '
<p>'.$this->Html->image($story['image'].'.jpg', ['alt'=>'image for article']).'</p>
</div>
<div class="large-6 columns">
<h5>'.$story['title'].'</h5>
<p>
<span><i class="fi-torso"> Creator: '.$story['creator'].' </i></span>
<span><i class="fi-calendar"> '.$story['created'].' </i></span>
</p>
<p>'.$story['story'].'</p>
';
}
?>
<div style="padding:0px;margin:0px;margin-bottom: -35px;">
<ul class="pagination" role="navigation" aria-label="Pagination">
<li><?php echo $this->Paginator->prev(' < ' . __(''));?></li>
<li><?php echo $this->Paginator->next(' > ' . __(''));?></li>
</ul>
</div>
put a link to success_story(div) as #(indicator)
Quick version: How do I output a taxonomy term ID in a views template? I just want the numeric ID value. This will be used as a link anchor.
Long version:
Scenario, I have a view which displays a list of taxonomy terms. That view has a Page and a Block. The Page view is set to display the Block view as a header. That Block view simply contains the taxonomy names. The Page view displays all of the taxonomy content.
I want the Block view list to anchor to the items in the Page view:
This view is already built, the missing part of the equation is getting the anchor links in place.
The view currently comprises 3 custom template files:
views-view-fields--categories--page.tpl.php
<article id="NEED THE TERM ID HERE">
<header>
<h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
</header>
<div class="table">
<div class="table-cell">
<?php print $fields['field_category_image']->content; ?>
</div>
<div class="table-cell">
<?php print $fields['description']->content; ?>
</div>
</div>
</article>
views-view-fields--categories--block.tpl.php
<li><?php print $fields['name']->content; ?></li>
views-view--categories--block.tpl.php
<ul>
<?php print $rows; ?>
</ul>
I've tried using a views contextual filter rewrite on the top block view links, with no luck.
All I need is the variable for the TERM ID - I've done a var dump of the available variables, I can see the TID in that list, but have no idea how to reference it in a views-view-fields template file and can find nothing online that answers this most simple of concepts.
Screenshots of the Page and Block view setup:
Finally won my argument that this jump list is completely redundant and stupid, so it'll be removed, however, I did manage to output the TID, which was fairly obvious, as these things often are...
views-view-fields--categories--block.tpl.php
<li>
<?php print $fields['name']->content; ?>
</li>
views-view-fields--categories--page.tpl.php
<article id="cat<?php print($view->result[$view->row_index]->tid); ?>">
<header>
<h2 class="flag-heading"><?php print $fields['name']->content; ?> <span>in association with <?php print $fields['field_sponsor']->content; ?> </span></h2>
</header>
<div class="table">
<div class="table-cell">
<?php print $fields['field_category_image']->content; ?>
</div>
<div class="table-cell">
<?php print $fields['description']->content; ?>
</div>
</div>
</article>
The variable is obviously in the view array, it was just a case of getting the index of the current view item.
Adding
<?php print $fields['tid']->content; ?>
should give you the TID in views-view-fields--xxx--page.tpl.php and --block.tpl.php
Make sure the fields are set to remove any default wrappers and you should be good to go.
I am trying to pull some information from my database and put it in a modal. I went to the foundations website and tried to figure it out from their docs section. I dont exactly understand it. So I have a section of my site that allows users to request to delete a song they uploaded. Now if they click the X a modal should pop up and ask to confirm.
<div class="row">
<div class="large-8 column musicup">
<p> <?php echo "No music uploaded..."; ?> </p>
</div>
</div>
<?php
}else{
?>
<h2 style="margin-top:1em;">Music uploaded</h2>
<hr style="opacity:.4;">
<?php
while($row_a = mysql_fetch_array($res))
{
?>
<div class="row">
<div class="large-4 column musicup">
<p><?php echo $row_a['title']; ?></p>
</div>
<div class="large-3 column musicup"><span data-tooltip class="has-tip tip-top" title="<?php echo $row_a['reason']; ?>">
<div class="button <?php echo $row_a['status'];?>"><?php echo $row_a['status'];?></div>
</span></div>
<div class="large-3 column musicup_date">
<p><?php echo date('F j Y',strtotime($row_a['uploaded'])); ?> </p>
</div>
<div class="large-2 column musicup">
<p>X</p>
</div>
</div>
<?php
}
}
}
?>
</div>
So now I have the modal and all the database queries on a new page called song_delete.php.
Here is the code for that:
<?php
include_once "functions.php";
$query = sprintf("SELECT * FROM songs WHERE user_id = %d AND song_id = %d",$_SESSION['user_id'], $_GET['id']);
$res = mysql_query($query) or die('Error: '.mysql_error ());
$row_a = mysql_fetch_assoc($res);
$totalRows_a = mysql_num_rows($res);
?>
<div id="deleteMusic" class="reveal-modal medium">
<h2>Request to delete<span style="color:#F7D745;"> <?php echo $row_a['title']; ?></h2>
<p class="lead">Are you sure you want to delete this song? Please allow 2 full business weeks for deletion.</p>
<span style="float:right;">Cancel
Submit </span>
<a class="close-reveal-modal">×</a>
</div>
Thanks for any help in advance. I appreciate it.
Please dont tell me about the mysql_query and how I should use PDO or MySQLi and OOP i know this, but this site is not currently coded with all that..
OK first things first - its often better to look at the compile source (HTML Source Code) in these cases. Can you do this? From the code you've given it looks fine but without the css/js linking and showing the placement of the reveal code there's no way to tell.
How Foundation Reveals Work
1 - The modal code is placed just before the ending </body>.
2 - It should look something like this:
<div id="myModal" class="reveal-modal">
<h2>Awesome. I have it.</h2>
<p class="lead">Your couch. It is mine.</p>
<p>Im a cool paragraph that lives inside of an even cooler modal. Wins</p>
<a class="close-reveal-modal">×</a>
</div>
3 - Depending on the size you want you can use an extra class of .small (for a reveal size of 30% browser width. Or one of these (taken directly from Foundation Docs)
.medium: Set the width to 40%.
.large: Set the width to 60%.
.xlarge: Set the width to 70%.
.expand: Set the width to 95%.
4 - At this point you can attach data-reveal-id="<id of modal here>" or call the modal via foundation. At this point your modal will popup in all Foundation 4 supported browsers. However you need the javascript files to close it.
5 - Now make sure you have the necessary scripts
<!-- If running version with default scripts -->
<script src="foundation.js"></script>
<script src="foundation.reveal.js"></script>
6 - Then call $(document).foundation() and then via the magical jQuery javascript library it should work as intended :-).
Extras
You can add extras attributes to reveal if you wish this way (List of all the attributes
):
$(document).foundation('reveal',<options here>,<callback>)
Lastly you might want to take the ajax tag off this (you aren't calling any content in asynchronously - it's all compiled at runtime via your server
Okay so, I'm working with CodeIgniter. posts.php is my view that displays all the posts, each post must display its corresponding comments, which is what I'm trying to achieve.
I have a method in my model that takes the postid($postid) and return its corresponding comment($comment), unless I call the model method via a controller method,how do I accompolish this?
This is my view :
<body>
<?php foreach ($post as $key):?>
<div class="container">
<div class="span10">
<div id="box" class="alert-message block-message info">
<div id="post" class="post">
<?php echo $key->content;?><br />
</div>
<div>
<p><?php //echo $comment;?></p> <!--HERE THE COMMENTS OF THE CORRESPONDNING POST MUST BE ECHOED-->
</div>
<div>
<p><em>Comment</em></p>
</div>
<div id="commentarea<?php echo $key->postid;?>">
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/comments/$key->postid",$name);
$data=array(
'id' => 'input',
'name'=> 'content',
'rows' => '2',
'placeholder' => "Write a comment...",
'autofocus' => 'TRUE'
);
echo form_textarea($data);
?>
Comment
<?=form_close();?>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
$("div#commentarea<?=$key->postid;?>").hide();
$('a#commentnow<?=$key->postid;?>').click(function(){
$("div#commentarea<?=$key->postid;?>").slideToggle(250);
});
});
</script>
<?php endforeach;?>
</body>
This is my controller method that returns the comments that corresponds to the postid:
public function comments($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
I'm a newbie,pardon me if my code is bad. I'm always looking forward to improving my code> Thanks for being patient. :)
Looking at your code it seems to me that you haven't fully understood how to use the MVC.
Firstly, your controller method comments contains the extraction of comments AND adding comments to a post. This doesn't seem logical when taking a look at the view file.
Instead you should seperate those two.
In your controller, add another metod called *post_comment* and move the adding comment functionality to that method and add a redirection afterwards:
public function post_comment($postid)
{
//Post Comment
$comment=$this->input->post('content');
$data=array('content'=>$comment,'comment_postid'=>$postid);
$this->comments->postcomment($data);
redirect('welcome/comments'); //redirect back to comments
}
Now, remove the adding of a comment from your comment method in the controller, so that you only retrieve the comments:
public function comments($postid)
{
//Retrieve
$comments['comment']=$this->comments->retrieve($postid);
$this->load->view('posts',$comments);
}
And finally change your view file - you need to post the comment to a new URL:
<?php $name=array('name'=>"form$key->postid");
echo form_open("/welcome/post_comment/$key->postid",$name);
This should do the trick.
how to create a widget out of an existing login action view ?
this is what I currently have in my login.php view file of the site's controller actinLogin()
<div id="login-wrapper">
<div class="login-container">
<?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?>
<div class="login-input">
<p>
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('placeholder'=>'username')); ?>
</p>
<p>
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('placeholder'=>'password')); ?>
</p>
<div id="bmenu">
<ul class="menu">
<li class="register"><?php echo CHtml::link("Registration", array('wsmembers/register'));?></li>
<li class="login"><?php echo CHtml::submitButton('Login',array('id'=>'login_button')); ?></li>
</ul>
</div>
</div>
<?php $this->endWidget(); ?>
</div><!-- .login-container -->
<div class="login-bg-bottom"></div>
if I click the default login link from the navbar, that's the only time that code above shows
the login box at the upper right corner of the page
but the problem is, the login box should be at the homepage and must already be there
without clicking a login link at the navbar. so how am i gonna do that? this have something to do with main.php layout file right?
Indeed.
What you have defined is a view that is used for content. If you click the link the login action will be executed and it it will show the form.
If you want this to be shown always, just do a Yii::app()->controller->renderPartial on the view. Note that it cannot be $this->renderPartial as you would normally do since the main layout is not executed by the controller. I usually define an alias called "userViews" that points to protected/views so I can do:
Yii::app()->controller->renderPartial('userViews.site.login');
Or something like that. Hope that helps :)
As for the alias you can add this to your config:
Yii::setPathOfAlias('userViews', dirname(__FILE__) . '/../../protected/views');
This is assuming your views are indeed under protected views.