Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is possible get list of pages? #123

Closed
descovi opened this issue Jan 3, 2014 · 12 comments
Closed

Is possible get list of pages? #123

descovi opened this issue Jan 3, 2014 · 12 comments

Comments

@descovi
Copy link

descovi commented Jan 3, 2014

Is there a way to get a dynamic list of pages?

@harlow
Copy link
Contributor

harlow commented Jan 3, 2014

Hi @Simonini currently this isn't a feature of High Voltage. Can you explain the use case you are trying to achieve?

@descovi
Copy link
Author

descovi commented Jan 3, 2014

Hi @harlow ! I would like output an html list of current pages.

@harlow
Copy link
Contributor

harlow commented Jan 3, 2014

You could use Ruby to list all of the file in a directory. However... you'd still only have the paths of the files and not the meta-data (title, etc) from the pages. What is the use-case for this? Is it a sitemap?

@davity
Copy link

davity commented Jan 16, 2014

HI @harlow, same question here, in my case I want to do a sitemap in xml for a website. I just need the URL for every page to get a result like

<url>
<loc>http://www.myawesomesite.com/about</loc>
<changefreq>weekly</changefreq>
<priority>1.00</priority>
</url>
...

from (though) something like

@static_pages.each do |page|
  xml.url do
    xml.loc "#{page_url}"
    xml.changefreq("weekly")
    xml.priority 0.9
  end
end

It's some way to do that?

@cleytonmessias
Copy link

+1

@harlow
Copy link
Contributor

harlow commented Jan 17, 2014

Hi @davity you could write small class that will traverse the app/views/pages directory and return the paths of all the static pages. Then those file paths (strings) would then need to be slightly changed (remove extension, etc) before passing to the page_url method.

If you need some help writing the Ruby let me know. If it works as expected we could consider creating a PR for the HighVoltage gem. Maybe the interface would look like @static_pages = HighVoltage.page_ids

@davity
Copy link

davity commented Jan 27, 2014

Hi @harlow, sorry for the delay, I was quite busy last week.
I definetly will need help with the ruby code (I'm new at ruby and rails). I've been trying to implement some code to get all the pages names but I have not much success. I think the result returned by the code should be something like

["about-this-site", "cookies", "more/your-rights", "more/even-more/about-us"]

And then it will be easy to iterate over the array and get all the routes

@kim3er
Copy link

kim3er commented Aug 7, 2014

This would deal with the root of the pages directory.

Dir.entries(Rails.root.join('app', 'views', 'pages')).map { |x| x.sub(/\.html\.[a-z]+$/,"") }

@kim3er
Copy link

kim3er commented Aug 7, 2014

  def get_pages
    pages = []
    Dir.entries(Rails.root.join('app', 'views', 'pages')).each do |f|
      matches = f.match(/\.html\.[a-zs]+$/)

      if matches.nil?
        next
      end

      filename = f.sub(matches[0], "")
      title = filename.titleize
      path = page_path(filename)

      pages.push( { title: title, path: path } )

    end
    pages
  end

Returns:

[{:title=>"About Us", :path=>"/pages/about-us"}, {:title=>"Address Book", :path=>"/pages/address-book"}]

@harlow
Copy link
Contributor

harlow commented Aug 11, 2014

Thanks for posting some code @kim3er.

Looks like the /page would also need to be removed in order to use the :path in the link_to. i.e. link_to page[:title], page_path(page[:path]) which would expect about-us for the About us page.

Hopefully this will be enough code to get everyone rolling along. Closing the issue.

@harlow harlow closed this as completed Aug 11, 2014
@jeremysmithco
Copy link
Contributor

@harlow Would you still be open to a PR for this functionality, provided it was good quality and the interface you want?

@phantomwhale
Copy link

Would love to see a PR for this - we are generating sitemaps too. Thanks @kim3er for the snippet to do this for us, for now. And advance thanks to @bentoncreation for any PR submitted.

[edit] I've taken the liberty of tidying up @kim3er's code into a drop-in module you can pop into your rails lib directory, and then call HighVoltagePagesHelper.static_pages from - https://gist.github.com/phantomwhale/7c1b0a8a119fce7d24bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants