how can i retrieve 10 random documents on docpad? - docpad

I have a folder called 'clients' with 30 images inside, and i want to return from docpad 10 random images every time I load the page. is that possible? I've searched for returning random stuff with docpad and didn't find any documentation about it.

The docpad skeleton "Kitchen Sink" has an example of generating a random number on each page load. See the Dynamic Content page.
The key point is to set the dynamic property in the page metadata to true (dynamic: true). You will also need to make sure you have the docpad plugin "cleanurls" installed (docpad-plugin-cleanurls) - not immediately obvious.
After that its just a matter of using the standard javascript Math.random. I'm using something like this to get a random post in my .eco file
<%=i = Math.floor((Math.random()*10))%>
<% document = #getCollection('posts').toJSON()[i] %>
Hope that helps
Edit - Jade Example
- var i = Math.floor((Math.random()*10))
- var doc = getCollection('posts').toJSON()[i]
The dash before the 'var' statements is important. see https://github.com/visionmedia/jade#a8

Related

Gatsby: How to display a list of images specified in a yaml file?

I use the current version of Gatsby (2.x) and want to use gatsby-image for rendering a gallery for products.
I have several YAML files for products. I can already create pages with the text content of these files but I also want to add a small gallery with images specified in the .yaml file.
An example YAML file looks like this:
product: "Some product"
description: "It is really awesome!"
screenshots:
- /img/product1/screenshot1.jpg
- /img/product1/screenshot2.jpg
- /img/product1/screenshot3.jpg
My problem now is that I can get the screenshots only as strings but I have no idea how to pass them to for rendering.
I thought of creating a component that takes the file name and uses a query to get the image data - but it can't take any parameters because it can only use static queries.
I've also not found a way to pass results from the first graphql query to a second for the image data.
If you install gatsby-transformer-sharp and gatsby-plugin-sharp and use a correct path to your images, Gatsby will automatically pick those up and pipe them through sharp, hence you can query those images. You can have a look at one of my sites which also uses a YML file with image paths that I then use with gatsby-image: https://github.com/LekoArts/gatsby-starter-portfolio/blob/master/src/sites/sites.yaml

Add posts to Jekyll index page, without .md files

I would like to write a generator plugin to add some post-like items to my blog. The items are supposed to appear in the blog index, but they have no page associated to them (you can't click on them).
I know I need something like
class QuoteGenerator < Generator
safe true
def generate(site)
# add a single post
site.posts << QuotePost.new(site, site.source, "Blub")
end
end
But what I don't understand is how to implement my Post subclass. I've found that other plugins (like this one to embed Flickr photos) write the data they want to a markdown+YAML file, and then reference this file:
class QuotePost < Post
def initialize(site, base, title)
# Nooo, I don't want to create a .md file for this
name = "2016-05-13-test.md"
dir = ""
# (write out .md file here)
super(site, base, dir, name)
end
end
But then, I would hardly need a Plugin in the first place. I could just generate the markdown files myself (with an external script).
What I'd like to do is to just set a couple of variables in my Post subclass, and have them available in the template for the blog index. How can I do that?
The case you've described looks to be unrelated to Post, since usual posts are file-based. Collections may be suitable, but again - that's not clear how you getting the content.
I'd suggest two major options:
Use _data/ to set an object list, where each item has the required properties if you can define them via a static JSON/CSV/etc file (or generate it once, to separate external data producer and jekyll visualization).
Use :pre_render hook and a plugin if you have to define the data via code -
your hook will also receive a payload hash as a second parameter which allows you full control over the variables that are available while rendering
Having your data in site.data variable allows you to iterate through items, render something or include a template and so on. Also there are plugins which generate new pages based on site's data.
The right answer depends on how you're getting the content, which markup you need and how those items will be used.

Path to CKEditor in CakePHP app on Zeus server

I'm integrating CKEditor into a CakePHP app which is running on a Zeus server (and therefore can't use .htaccess - I have to use rewrite.script instead). Problem is, the paths that CKEditor puts into the head section of the page don't work, so the editor won't load.
For instance, one generated path is:
http://www.example.com/js/ckeditor/config.js?t=B8DJ5M3
If I go to
http://www.example.com/js/ckeditor/config.js
I can see the file, but as soon as I add the ?t=B8DJ5M3 on the end, Cake complains that it can't find the jsController.
I'm not sure what to do about this - whether to dig around in CakePHP, CKEditor or the rewrite.script files! What should I try next?
That query string on the end of your URL is used to make sure the file isn't cached. Seems like something in your GET request configuration/routing on the Zeus server is trying to locate that exact file including the query string. You're going to need to create a Rewrite that performs a goto on the URL minus the query string. I found a pretty solid article in the Drupal forums where someone put together a script that may help you: http://drupal.org/node/46508
RULE_0_START:
# get the document root
map path into SCRATCH:DOCROOT from /
# initialize our variables
set SCRATCH:ORIG_URL = %{URL}
set SCRATCH:REQUEST_URI = %{URL}
# see if theres any queries in our URL
match URL into $ with ^(.*)\?(.*)$
if matched then
set SCRATCH:REQUEST_URI = $1
set SCRATCH:QUERY_STRING = $2
endif
RULE_0_END:
And from there, handle your goto minus the query string. Hope that helps
You can do this
In the view where you want to display the editor, put the following script on the top of the page (or somewhere before textarea which you want to contain editor):
<?php echo $this->Html->script('ckeditor/ckeditor');?>
This scipt will include the "webroot/js/ckeditor.js" file to your view.
Create the textarea and give it a class named "ckeditor"
<?php echo $this->Form->textarea('content',array('class'=>'ckeditor'))?>
Voila! The editor is now displaying instead of raw textarea.

Changing Views in a Module pops me into the Admin Skin

This question has probably been the most covered question in all of DotNetNuke's lifetime but I'm going to ask it here in StackOverflow because I need an answer, a really good one that doesn't make me look the other way. Thanks in advance to all DNN experts.
I've researched many ways of making this work for me and i've seen Michael Washington's solutions (Panels, MultiViews, ...) and Will's (Strohl) blog post on DotNetNuke's personalization engine through setting SkinSrc which is useful, as well as reading through Default.aspx's code which has given me more insight, however, i'm still faced with the problem that calling EditUrl()/NavigateUrl() brings me to a page with a single module in admin skin or a page with nothing respectively.
The specific version is DotNetNuke 6.0.1 (DNN). This Module has 4 other views in addition to the main view which I desire to navigate through sequentially. e.g.
Begin Checkout -> Collection of Delivery Details -> Confim Order
Have you found a solution?
I want to achieve
1) Module loads with other modules around. No module isolation
2) Views in a module that don't Preload e.g. Page_Load in each view gets called when the Module loads up
Help!
Assuming you are asking this as the module developer, the solution is to not use DNN's mechanism for specifying a control. So, you can't use EditUrl or specify the ControlKey in the NavigateURL call (which both generate "ctl=mycontrol" in the URL). Instead you need to have your module display your various controls based on the Query String parameters. So, you'll generally have a control in your module who's primary purpose is to dynamically load other controls based on the query string. So, for instance:
You will start with your control that lists items. You'll have a "Buy Now" button for each item. The hyperlink for each item can be generated by calling
NavigateURL(TabID, "", "View=BeginCheckout", "itemid=" & id, "mid=" & mid)
2.) On the page load of the handler control, it looks to see if anything is specified for the "View" Querystring parameter. If not it displays the listing control, if so, it displays the corresponding control.
Dim controlPath As String
Dim path as String = "~/DesktopModules/MyModule/Controls"
Select Case Request("View")
Case "BeginCheckout"
ControlPath = path + "BeginCheckout.ascx"
Case "DeliveryDetails"
ControlPath = path + "DeliveryDetails.ascx"
Case "ConfirmOrder"
ControlPath = path + "ConfirmOrder.ascx"
Case Else
ControlPath = path + "ItemList.aspx"
End Select
If System.IO.File.Exists(Request.MapPath(controlPath)) Then
placeholder.LoadControl(controlPath)
Else
Throw New Exception("Unable to load selected template. Please go into module settings and select a list and details template.")
End If
Most of the advanced modules for DNN do something along these lines so there's plenty of sample code out there. I would guess some of the core modules do something similar. I adapted the code above from Efficon's Articles module for DotNetNuke.

Can a Joomla module "know" what position it's in?

I'm fairly new to Joomla (I've been more of a Wordpress guy) and I have a question about module positions.
Can a module know what position it's in. For instance can I do something like this:
if(modulePosition =='left'){
Do this...
}else{
Do that...
}
It seems easy enough, but I've searched for hours and can't find anything that will help me with that. I know there is a countModules function but from what I can tell, that just checks to see if the module is active or not.
Thanks for your help!
I found the answer! Mostly thanks to #Hanny. His idea of using the modules id got me googling for that and I came across the answer. For anyone else that happens to be looking to do something similar here it is.
You use a global variable $module (who'd a thought, right?)
So my code now looks like this:
$class = '';
if($module->position == 'position1'){
$class = 'class1';
}
and so on...
Pretty simple, huh?
To find out what else you can do with the global variable $module just put this in your code and see what info you can use:
echo(print_r($module));
Thanks for all your help!
The short answer is 'yes', you'll assign a module a position based on your template. When it shows up you can have conditionals like that regarding that position (different templates have different naming conventions for positions, so make sure you know what they are before coding).
For example, some use "Position12", others may use "leftcol", etc. You just have to check in the template files to see (you can check the .xml file in the template directory to see the positions listed in the template, or look in the index.php file for the jdoc includes).
In some of my experience, the only time you'll really ever need code like that is in the core layout files of the template (for example, if you have different widths of columns depending on modules being present or not), otherwise there won't really be a time where you 'may or may not' have a module showing up - because you'll explicitly be telling them where to be and when on the back end.
I tried to comment under john's solution but I don't have a enough rep points-- I wanted to add it doesn't matter what you name the module position in your template case-wise the position name you get back from $module->position is always all lowercase regardless of how you named the position in the template... ie. in your template xml somewhere you might have topBar position will be named 'topbar' not 'topBar' when you try to check it with
if($module->position == 'topBar') //always false... use instead
if($module->position == 'topbar') //what you need to use
I'm going to disagree with Hanny. I think the answer is no, not as you describe.
The template knows when it has reached a module position, and it gets a list of modules assigned to that position, then calls for them to be rendered. But it doesn't pass that information on. It's not stored in JApplication or JDocument etc either (like, nothing stores where in the template the rendering is up to or anything).
There are some hacky ways to almost get what you want though. If you know the template positions you need to search (sadly there's no easy method for getting this from the template - otherwise you could parse your template's .XML file for <position> elements...), then you could do something like:
<?php
$positions = array('left', 'right', 'top', 'bottom')
$found_in = false;
foreach ($positions as $cur_position)
{
$module_positions = JModuleHelper::getModules($cur_position);
foreach ($module_positions as $cur_module_in_pos)
{
if ($cur_module_in_pos->module == 'mod_MYMODULE')
{
$found_in = $cur_position;
}
}
}
if ($found_in)
...
Of course, this doesn't work well if your module is included multiple times on the page, but maybe that's an assumption you can make?
Otherwise it'd be up to hacking the core - you could use a JDispatcher::trigger() call before the template calls a module, set some var etc. Unfortunately there's no such event in core to start (a pre_module_render or something).
A module instance is assigned to a single position and this is stored in the database and normally you would style the position in the template. A module instance can only be assigned to one position. So while it's an interesting question, it's not really a practical one.
The exceptions to this are the following:
loadposition ... you might want to know if a module is being loaded using the plugin because this would put it potentially somewhere besides the styled area for the position. THough i would recommend always making a new instance for this precisely so you have more control.
loadmodule ... module loaded by name using the plugin. In this case again you are probably better off making a new instance of the module and styling it. Also I'd put it in a span or div anyway, depending what it is.
jdocinclude:module ... loading a module directly in a template. Again if you are doing this I would wrap it in a span or div. In this case you are also allowed to include a string of inline styles if you like that kind of thing.
Rendering the module to a string and echoing it, again that is basically a very customized solution and you would want to set the styles and options.

Resources