*Snippet* - Dynamically add Topics/Pages to Infoblock and Menu

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
I did this a little out of frustration and slightly in a hurry, so excuse the control-side code...

Here is a snippet which will immediately populate the infoblock with new topics/pages added via the Content Management area in NopCommerce.

Rules - (or rather, the trick...)
All new Topics which you want automatically displayed in the Infoblock, you simply need to append "Page." to its NAME (not the page title, the topic(system) name)

So if I want to add a new topic "The Management Team" and have it linked automatically, I add a new topic, and NAME it e.g. Page.TheManTeam, then proceed giving the Topic Title - "The Management Team" and adding its content.

This is for a start, and can be ported into the classfile for InfoBlock rather easily and replaced client-side with a control instead (ye ye i know), but this was quick and easy and needed no recompiling :)

Code to be inserted in:
~/Modules/InfoBlock.ascx


I inserted it just below About Us at:

            <li><a href="<%=Page.ResolveUrl("~/aboutus.aspx")%>">
                <%=GetLocaleResourceString("Content.AboutUs")%></a></li>

<%
  var topics = this.TopicService.GetAllTopics();
    topics = topics.FindAll(t => t.Name.ToLower().IndexOf("page")==0);
    foreach (var topic in topics)
    {
        NopSolutions.NopCommerce.BusinessLogic.Content.Topics.LocalizedTopic localizedTopic = this.TopicService.GetLocalizedTopic(topic.TopicId, NopContext.Current.WorkingLanguage.LanguageId);
        if (localizedTopic != null && !String.IsNullOrEmpty(localizedTopic.Title))
        {
            string topicURL = SEOHelper.GetTopicUrl(localizedTopic.TopicId, localizedTopic.Title);
            string topicName = localizedTopic.Title;
%>
<li style="margin-left: 5px"><a href="<%=topicURL%>">
    <%=topicName%>
</a></li>
<%
        }
    }
%>




Hopefully you guys move further with it (keep it simple) e.g. it can also now be tweaked to populate the navbar with a drop down for instance. Even isolate those items further by creating a name rule for that block e.g. Page.TopMenu.MyTopicName, where the change is merely in:

topics = topics.FindAll(t => t.Name.ToLower().IndexOf("page.topmenu")==0);


... up to you :)

Enjoy!
13 years ago
mmm, no crit or comment on this? strange,  hope somebody finds it helpful lol
13 years ago
That's actually quite a cool concept.

I've done menu's in nopCommerce in so many ways, from simply doing topics with lists in, through to adding complex 'settings' that store (csv)arrays of topic names and get them on the fly.

The thing I always thought was a weakness in nopCommerce was the CMS aspects of the site. I know it's an e-commerce app [and by far the best IMHO] but it always felt a little short when compared to Umbraco for example, but you'd expect this as Umbraco is a different animal entirely.

Now I'm a bit more familiar with nopCommerce, it is actually quite straightforward to make it a bit more CMS like.

I like the idea of using dot separators and IndexOf() to filter them.

This could be extended to several menus quite easily, so you have Menu.Menu1.Page.Pagename for example, so you can be 'dynamic' very easily and this is loads better than my lists of page topicss in settings, and neater than my menus as a list in a topic.

I like :)
13 years ago
I have tried this in v.1.8 and get the following error message:

Error  1  'ASP.modules_infoblock_ascx' does not contain a definition for 'TopicService' and no extension method 'TopicService' accepting a first argument of type 'ASP.modules_infoblock_ascx' could be found (are you missing a using directive or an assembly reference?)  c:\***\NopCommerceStore\Modules\InfoBlock.ascx  14  21  NopCommerceStore


Any ideas?

Cheers - Helger
13 years ago
mm interesting...

I don't see much different in Infoblock.ascx/.cs between 1.8 and 1.9 (which i'm currently on)

Have you made done any other customisations?

From what I can see Infoblock.ascx.cs on v1.9 ultimately inherits BaseNopUserControl
which ultimately is using NopSolutions.NopCommerce.BusinessLogic.Content.Topics;

try adding the following to InfoBlock.ascx?

<%@ Import Namespace="NopSolutions.NopCommerce.BusinessLogic.Content.Topics" %>


Let me know how it runs!
13 years ago
hannibal_za wrote:
mmm, no crit or comment on this? strange,  hope somebody finds it helpful lol

I have been struggling with this very problem all day! And just found your comment..so, I am excited to try it! Crossing my fingers!
kristinart
12 years ago
Hi...

I have to tell that you have made a great job there saving a lot of my time.

I know it has been a while since you made the post but I have a question.

I succesfuly create a new topic but the page of the topic that was created is using the 3 column master page. Is there any way to change it to two columns as all my website is using two columns.

Thank you in advance
12 years ago
Ignore the question.. I fixed it by changing the MasterPageFile of the "Topic.aspx" to TwoColumns and is working like a charm...

Thanks a lot for the contribution :)
12 years ago
GetAllTopics()
returns 13 topics.

The next line doesn't return anything, can you please explain wha this is doing:
topics = topics.FindAll(t => t.Name.ToLower().IndexOf("page") == 0);

Many thanks
12 years ago
Ok, I get it now.
You have to name the topics you want in there as page.TopicName
Have it working now, thanks for your help.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.