Loop through mongoDB database arry using #foreach in express - html

Hi I have a database which I can access in a view using
html and express layout I think #each and {{}} is a js syntax
{{posts[0].detail}} <----------------This works
But I want to display all the contents of the post using
#each(post in posts)
{{post[don't know what to put here].detail}}
#end

What about {{post.detail}} from #Cristy
So this answered my question.
I thought #each(post in posts) <----- you had to use posts.detail
Now I know post takes the attribute of posts.
Ex:
#each(post in cars) <------ would still be post.detail as well.

Related

How can I get the innerText of Dynamic Html tags using Puppeteer.js (node.js) in TripAdvisor?

How would I get all 10 comments located in this page with a loop or a Puppeteer function https://www.tripadvisor.com/Restaurant_Review-g294308-d3937445-Reviews-Maki-Quito_Pichincha_Province.html using innerText property?
The only solution I have come up with is getting the outerHTML of the whole container of comments and then try to substring to get all the comments, but that is not optimal and I think its a more difficult approach. Maybe there is an easier solution in Puppeteer I cant find?
I am doing this for educational purposes. The comments are in class="partial_entry" and I want to get the innerText of a Dynamic Html tag (I want all 10), like the ones you see here:
If I where to open the div that contains <div class="review-container" data-reviewid="606551292" data-collapsed="true" data-deferred="false"><!--trkN:3-->, I would get another with id="review_582693262". Getting to the point, If I get to a <div> that has class="partial_entry" this would be where my comment is located. I have tried a few things but I get null, because it is not found since the parent <div> for each comment has a unique id like id="review_xxxxxxxxx".
Its kind of difficult since the review id is autogenerated like id="review_xxxxxxxxx" and cant iterate with a loop copying the CSS path since I dont have a static parent .
Why not just select those elements which have partial_entry class? This works:
let comments = await page.evaluate(() =>
[...document.querySelectorAll(".partial_entry")].map(item => item.textContent)
);

Using API to fetch data from one site to display on another

I have a site on Wordpress where I am trying to find the best way to create a dropdown which displays data from a custom taxonomy to eventually integrate it into a different site (also on Wordpress).
Where I have go to is trying to obtain the correct Routes/URL's to fetch this information.
I have a Post type called listings which has a taxonomy called listing_area which has different areas where posts are associated, e.g. Wales, East Anglia.
I have got so far that I have decided to use the Plugin WP-API (whether this is the right thing I don't know, I am aware that Wordpress now had an API in it's new update). I have managed to get this URL working and pulling in the terms of listing_area - http://scd.blaze.wpengine.com/wp-json/taxonomies/listing_area/terms/174
This is the test page I have going which is linking to these URL's in turn -
http://scd.blaze.wpengine.com/test/
I have no idea if I'm doing the right thing here and I have very basic knowledge on it and would hugely appreciate it if someone could point me in the right direction!
Thanks
You're going on the right path, but I suggest to work straight with the fresh Wordpress REST API if you can upgrade your websites to 4.4. Otherwise you can still use your REST plugin as it is pretty much the same. I'll try to explain how to go through what you want to achieve (navigate through terms of a distant Wordpress website and display posts related to this terms).
Get the terms from the other WP
Using the new WP REST API, here is a small function that you can use to get your taxonomy terms:
public function getDistantTerms($taxonomy) {
$response = wp_remote_get('http://www.yourwebsite.com/wp-json/wp/v2/terms/' . $taxonomy);
if(is_wp_error($response)) {
return array();
}
return json_decode(wp_remote_retrieve_body($response));
}
Here I make use of wp_remote_get function to get the JSON return from the REST function terms by passing it as parameter the taxonomy slug (ex:listing_area) - here is a demo of what it returns. Add this function to your functions.php then use it in your template to build up your select:
<select name="term_select">
<option value="">Please choose a term</option>
<?php foreach(getDistantTerms('listing_area') as $term): ?>
<option value="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php endforeach; ?>
</select>
It seems that's pretty much what you actually got.
Link your select to a custom template
So the next step is to redirect to a page that list the posts of the term you choose. First we handle the redirection in JS:
$('select[name="term_select"]').change(function() {
if($(this).val() != "") {
window.location = "/show-post-term/" + $(this).val();
}
});
We add a little rewrite rule to redirect this url (change it to whatever you want) to a template we'll name distant-posts.php (all of this take place in your theme functions.php):
1. Add the rewrite rule
add_action('init', 'distantposts_rewrite_rules');
function distantposts_rewrite_rules() {
add_rewrite_rule('show-post-term/([^/]+)/?$', 'index.php?term_slug=$matches[1]&distant_post=true', 'top');
}
2. Add two query vars
add_filter('query_vars', 'distantposts_query_vars' );
function distantposts_query_vars($vars) {
$vars[] = 'term_slug';
$vars[] = 'distant_post';
return $vars;
}
3. Redirect to the template if query vars are set
add_filter('template_include', 'yourpluginname_blah_template_include', 1, 1);
function yourpluginname_blah_template_include($template) {
global $wp_query;
$distant_post = $wp_query->query_vars['distant_post'];
$term_slug = $wp_query->query_vars['term_slug'];
if($distant_post && $term_slug) {
$tpl = locate_template(array('distant-posts.php'));
if(!empty($tpl)) {
return $tpl;
}
}
return $template;
}
So in short what we're doing here: we add a rule that handle the /show-post-term/term-slug URL by redirecting it to index with two query vars: one that tell we're in a "distant posts" mode and one that carry the term slug. Then we declare those query vars to Wordpress, and use them to change the template that Worpdress should display if they're set.
List the distants posts from the taxonomy term
Back to the REST API. We use the GET REST function posts by passing it as GET parameters the taxonomy name as key, and the term slug as value. Here is a demo of what kind of return you get.
An important note before going further: after you updated to WP 4.4, you need to change your taxonomy declaration in order to make this work. You need to add to your declaration the parameter show_in_rest set to true, and set query_var to true.
So we add this little function to functions.php to retrieve the posts from the template:
public function getDistantPosts($taxonomy, $term) {
$response = wp_remote_get('http://www.yourwebsite.com/wp-json/wp/v2/posts?' . $taxonomy . '=' . $term);
if(is_wp_error($response)) {
return array();
}
return json_decode(wp_remote_retrieve_body($response));
}
And then in your template, you call it this way:
global $wp_query;
$posts = getDistantPosts('listing_area', $wp_query->query_vars['term_slug']);
Then use the $posts array to display your posts (it contain regular post objects).
Going further
A few things that you may want to do now that you have the context established:
Add cache to the REST return
In order to avoid to overload your main website server, you should really consider caching your REST calls results. I will not detail this here as there is some work to do on it, but a good start could be this script.
Add pagination to your distant posts template
If you have a lot of posts associated to your terms, you might want to add a pagination. You can change a bit the distant posts REST function to add the page parameter for this - see the documentation.
Add a "single page" for your distant posts
You might want to have individual pages for your distant posts on your main website, as the text might be too long for the list mode. You can start on the distant-posts.php code and add a post_id query var, then use the REST posts function to get your post like this : /wp-json/wp/v2/posts/<post_id>
To understand the basics of the WP REST API I strongly suggest you to visit the wp-api.org website. There is a pretty good article on the REST API on wpmudev.org that you can read too. If you need to learn about the REST basics, I suggest you to read the Wikipedia post about it.
Hope you'll manage to get through this, have fun!
I found this this url got me the results I needed -
http://scd.blaze.wpengine.com/wp-json/posts?type=listings&filter[listing_area]=channel
my post type being listings and the slug of my term channel

Decoding HTML tags in Laravel 5

I am using CKeditor for my Laravel 5.1 custom CMS. When I request content of stored pages in the database on my view, they appear with HTML tags like <p>Hello world</p>.
I have used html_entity_decode() with and without the charset specified to no avail.
It is also worth mentioning that I use Blade template engine at my view. Hence, my demo code looks like
$post->content = '<p>Hello world</p>'; // from the database from controller
{{html_entity_decode($post->content)}} //did not decode
I also tried using it in my controller instead and it did not change anything like this
$post->content = html_entity_decode($post->content); //before sending it to the view
I need your help to address this issue.
Instead of doing {{html_entity_decode($post->content)}},
try this:
{!! $post->content !!}
More info here https://laravel-news.com/2014/09/laravel-5-0-blade-changes/
you can do this in the controller
strip_tags($post->content);
or if you use in blade
{{ strip_tags( $post->content ) }}
see more info here strip_tags

Binding dynamically within an ng-repeat expression

For a TV Guide, I am trying to create a dynamic expression within an ng-repeat directive as follows:
<div ng-repeat="programme in programmes['{{channel}}-wed-jan-14']" alt="{{channel}}">
{{channel}} in my controller should evaluate to something like "eTV". The binding is working fine with the alt="{{channel}}" instance but not with the array instance. Angular simply serves up the line of code commented out. If I hardcode the "eTV" string in place of the {{channel}}, it works fine.
Am I trying to ask Angular to do what it is not designed for, or is it possibly my array handling which is dodgy?
Okay, not sure if I just asked a dumb question, but in the absence of responses, I managed to figure out a solution by writing a filter as follows:
Template:
<div ng-repeat="programme in programmes | getChannelDay:channel:dayString" alt="{{channel}}">
Controller filter:
app.filter('getChannelDay', function() {
return function(programmes, channel, dayString) {
return programmes[channel + dayString];
};
});
The issue with my initial problem
<div ng-repeat="programme in programmes['{{channel}}-wed-jan-14']" alt="{{channel}}">
is that I was trying to put {{channel}} inside the expression, but that is the format for markup.
I tried to use the following instead:
<div ng-repeat="programme in programmes['channel + daystring']" alt="{{channel}}">
but I am doing something wrong here. I am pretty sure there is a way to get this to work - if anyone knows, please comment.

Umbraco 7 Multiple Textbox pulling "System.String[]"

I am currently using Umbraco 7.1.8 and I have just finished my final template however I wanted to create a tag like list for the client to add in as they wish.
I have a multiple textbox with the alias workUndertaken however when I call it, it echos System.String[].
My code is pretty simple - I called it two different ways to ensure it wasn't an issue with one method.
<p>#Model.Content.GetPropertyValue("workUndertaken")</p>
<p>#Umbraco.Field("workUndertaken")</p>
Does anyone know where I am going wrong?
Sorry got the answer almost instantly. Was hard to find.
So I will post the answer with the code I am now using. Hopefully others find this useful.
#{
if (Model.Content.GetPropertyValue<string[]>("workUndertaken").Length > 0) {
<ul>
#foreach (var item in Model.Content.GetPropertyValue<string[]>("workUndertaken")) {
<li>#item</li>
}
</ul>
}
}