change layout content from controller - layout

I am using Yii. I want to have a dynamic link on a layout. This dynamic link will be modified by controllers. Let's say that dynamic link uses a user's id given by controllers to perform a task.
I am thinking to use jQuery script to get user id returned by controllers then use the user id to modify a div that holds the dynamic link.
What do you think about this technique?

It seems like you want to dynamically change a link AFTER the page is rendered, with client-side JavaScript. But it makes more sense to dynamically render a different link the first time, during the server-size PHP rendering process. The controller generates the view, after all! I would get the user ID from the controller during the page request, pass the ID in to the view, and then build the link in the view dynamically on the initial page load.
If you are modifying a link in a layout (not a view), then the best thing to do is create a variable in the Controller, and set that variable with the view. Look at how Yii uses the $layout, $menu and $breadcrumbs variables to do this.
Assuming that the user is logged in and you want their ID, you can get the ID from the Yii::app() object as well, like so:
<?php echo CHtml::link('Edit user',array('user/edit','userId'=>Yii::app()->user->id)); ?>
But at that point, you can just request the user's ID in the controller, and don't need to build a link like this.
Assuming that you want a different user ID than the logged in user, pass that ID ($userId) from the controller into the view, and just do this (as Moyersy said):
<?php echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>
This will build the following link (where $userId = 99999999):
Edit user
So when the linked is clicked, in the actionEdit() you now have access to the user's ID via the GET variable $_GET['userId'].
NOW, if what you want to do is change an already created link, then you would need to use jQuery. But you will need to explain in more detail why you are doing this and what is triggering the link change (a dropdown menu?).

I'm sorry, I can't understand what you are trying to do. Specifically I don't understand what a dynamic link is.
Edit:
<? echo CHtml::link('Edit user',array('user/edit','userId'=>$userId)); ?>

Related

Automatically provide data to Express partial views

I'm writing an Express 4 app, and am currently using twig.js as the view engine since I find it comfortable, though I could be persuaded to change this engine.
I've done a lot of development with PHP/Laravel and have gotten used to what in that camp they call view composers. Using these I can write a composer for a particular view, whether it's a main page view, a layout which other views extend, or a partial other views include. The composer does any necessary logic to prepare whatever data the view needs, and then attaches it to the view's context so it's available during rendering.
For example, I might have a partial which shows the current user status, so if they're logged out it'll maybe have just a log in button, and if they're logged in it'll have their icon, username, and a menu to let them log out etc. The corresponding composer would check to see if a user is logged in, and if so attach the relevant data about the user to the view's context. It then doesn't matter which page includes this partial; the data will always be available without me having to remember to add that specific data to the context passed to the page's main view.
Is there some equivalent in Express? Or does it depend on the template engine I'm using?
Currently, views that are rendered using res.render() need to have their data passed in on every res.render(). If the view uses a partial template that requires certain data points, the view that uses that template will need to have that value passed in through res.render(). So in short, you always need to pass the data in regardless if the data point is required by a partial template that might be shared. When using view engines, nothing is automatically provided to the view.
Are you asking if it's possible to have some view A, that includes partial P_A and be able to pass some data to the partial that's not dependent on routing, i.e wherever you are on the site / app your partial has data bound to it (totally independent of both the route / url and view ) which you could use for your logged in status ?
if so then create your partial, let's use a navigation / menu partial:
please note that this example uses ejs for templating
<nav>
<ul>
<!-- pass the data upon call / include-->
<% for(var i = 0, len=navigation.length;i < len;i++) {%>
<li><%=navigation[i].Text%></li>
<%}%>
</ul>
</nav>
Call / Include your patial on a view
view.ejs
<%- include('nav', { navigation : [{Text : 'Items' ,href : '/' },{Text : 'People' ,href : '/names' }] } )%>
To my knowledge there is nothing like that in expressjs.

Orchard CMS: Using different Layout for Anonymous users

I would like to use a different layout view for anonymous users. I have tried using url alternates and I am not sure how I can create a layout for anonymous users since there is no particular url for them.
The idea is that, if a new user visits the site I want to show a splash screen with very limited information with an option to register/ login to view the full site. This splash screen will have a completely different layout / look and feel from the rest of the site.
I tried using the Anonymous user layer but all I could do was move widgets (maybe I am missing something).
Any help would be greatly appreciated.
Thanks!
There is no out of the box solution but you can do something like this to get what you want:
Add new layout in a file with the name of your choosing - for example, you could name it Splash.cshtml.
Add the code below to the top of your Layout.cshtml template:
#if (WorkContext.CurrentUser == null) {
#Display.Splash()
return;
}
The code will check if the user is logged in. If it's not, it will render the content of your Splash.cshtml template, and stop the rendering of the rest of the Layout.cshtml template.
If you need to display any of the widgets in your Splash.cshtml template, you could add it by simply adding #Display(Layout.NameOfTheZone) where you should replace NameOfTheZone with the actual name of the zone you're using inside the Layout.cshtml template. Generally, anything that you use inside Layout.cshtml template with the Model object, you can use through Layout object inside any of your views.

Mixing Orchard CMS content with module

I am currently working on an Orchard module. This module contains an MVC application including the views. I would like to make the module as configurable as possible. One of the items that I would like the customer to configure is the way the MVC views from the module look. Part of it will be determined by the theme. But not everything. Consider the following scenario:
The module contains a view for placing an order. The view displays a form in ´normal view way´. That is field labels and input labels. But at the head of the form each customer must be able to define his own set of instructions to display. Or maybe the customer wants to put there a message for pointing the customer to some other actions.
In the most ideal way I would have a content page where the customer can put all kind of content and one specific block that is the result of the view of the module. Kind like a web part. I can´t find out if it is possible and how that is achieved.
Edit for clarification
Module creates a page like this:
TITLE
FORM
So both title and form are outputted by the module controller.
I have managed to create a layer with the condition that the url matches the page with my form.
I have added a HTML widget to this layer in the content zone with position 1 (tried 0 to).
However the pages looks like this:
TITLE
FORM
WIDGET
instead of
WIDGET
TITLE
FORM
Returning a ShapeResult from your controller action will ensure that your view is themed and benefits from widgets, which are your "kind like a web part" thingies in Orchard.

Modifying sharepoint edit dialog

I have successfully created a feature in sharepoint that modifies the existing edit dialog and adds a custom button to it like this.
and I am aware that I can pass back data when the user clicks the custom button like this.
<CommandUIHandlers>
<CommandUIHandler Command="ActivateUser" CommandAction="/_layouts/MyFeature/MakeUserActive.aspx?ListID={ListId}&ItemID={ItemId}&ItemUrl={ItemUrl}&ListUrlDir={ListUrlDir}" />
</CommandUIHandlers>
As detailed here
I can now handle the list item and perform my required actions on it BUT given that this button has been added in the modify context (IE: Inside the sharepoint edit item dialog) what if you want to save changes to the data itself?
To me it seems like using your custom button would always mean losing any changes the user has made to the data. Is there a way around this?
Good question!
You actually already linked to the solution: Right now you are simply redirecting the user by using a URL as your CommandAction: CommandAction="/_layouts/MyFeature/MakeUserActive.aspx?ListID={ListId}&ItemID={ItemId}&ItemUrl={ItemUrl}&ListUrlDir={ListUrlDir}"
This if course redirects the user to another page without saving the current entry. What you want to do is use Javascript as linked in the MSDN article:
CommandAction="javascript:alert('here be dragons');"
You can either work the the SharePoint Javascript object model here and use something like SP.ListOperation.Selection.getSelectedItems(); or you could use complete custom code.
From your aspx page name I can see you want to "make a use active" (btw: wouldn't "ActivateUser.aspx" be nicer?). If this simply means setting a property in another list you could do that with the SharePoint OM, if it is some custom stuff you would need a webservice which you can call from JavaScript and "activate the user" like that. You can of course always access the current form and pass on the values the user entered. Or you could create a custom save button which does some stuff (activate user) before saving.
Lastly: You can also have postbacks in your custom button where you could do anything you'd like.

How can I use base_path() inside a view in Drupal?

I have the following fields inside a view:
User ID
Teaser
I selected "exclude from display" for the User ID field.
In the Teaser field, I selected the "rewrite the output of this field" option. This way, I can use html to completely control the structure of the view's output. Moreover, I can call the User ID field by using it's token, [uid].
For example, I can do the following in the teaser's rewrite:
Visit Profile
The problem with the above code is that the path is relative. It will only work on the front page of the site.
For example, if this link is clicked in
www.example.com/node/1
then it will visit a non-existent URL of:
node/1/user/[uid]
I have tried the following: http://chopapp.com/#o7osql65
But views fields does not allow PHP codes.
I might be approaching this entirely wrong. Does anyone have any suggestions? Thanks.
You'll want to rewrite the output of the UID field, and have it be rendered as a link. Even if you exclude it from display initially, it will still be available as a link via its token to other fields. One of the options when selecting to 'output this field as a link' is to make the link absolute.

Resources