This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-02-05
Channels
- # bangalore-clj (2)
- # beginners (132)
- # boot (311)
- # cider (5)
- # cljs-dev (27)
- # cljsjs (2)
- # cljsrn (16)
- # clojure (76)
- # clojure-art (1)
- # clojure-france (8)
- # clojure-russia (35)
- # clojure-spain (3)
- # clojure-spec (2)
- # clojure-uk (11)
- # clojureindia (1)
- # clojurescript (98)
- # core-async (3)
- # css (9)
- # cursive (9)
- # datascript (7)
- # datomic (7)
- # emacs (30)
- # jobs (1)
- # lein-figwheel (7)
- # london-clojurians (1)
- # lumo (14)
- # off-topic (6)
- # om (1)
- # planck (3)
- # protorepl (1)
- # re-frame (27)
- # reagent (17)
- # spacemacs (10)
- # untangled (1)
- # yada (16)
I'm going to go ahead and make a folder in my bookmark bar 😄
How can I start a clojure atom off with a system property
How can I make the if then it a let work. I have now this :
(let [prevnext (if (< page 6) (- 11 page) else 6)
num-pages 466
page 466
lower (max 1 (- page prevnext))
upper (inc (min num-pages (+ page prevnext)))]
(range lower upper))
that's assuming page
is defined earlier somewhere else in the code. otherwise you will get an error since page
in the snippet above is defined after prevnext
. the order is important with the let
bindings
I have this function :
(ns paintings2.pagination)
(def pagination-numbers
[]
(let [page (data.current-page)
prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
num-pages 475
lower (max 1 (- page prevnext))
upper (inc (min num-pages (+ page prevnext)))]
(range lower upper)))
now I use it like this :
{% for number in (paintings2.pagination/pagination-numbers()) %}
@roelof You need to call the funtction in the clojure code that renders the selmer template. Check out the link I posted.
(render "{% for number in pagination-numbers %}" {:pagination-numbers (paintings2.pagination/pagination-numbers)})
@madstap : one question. How can I make it work that in the for number loop I made a <li>
@roelof Not really sure what you're asking... You want something like this <ul>{% for number in pagination-numbers %}<li>{{number}}</li>{% endfor %}</ul>
, right?
Can I do something like this :
(render "{% for number in pagination-numbers %} <li> .... </li>" {:pagination-numbers (paintings2.pagination/pagination-numbers)})
nope, when I place this :
(render "{% for number in pagination-numbers %} <li><a href="#"></a> number </li> {% endfor %}" {:pagination-numbers (paintings2.pagination/pagination-numbers)})
I see the code in my website not the numbers 😞I see now this in my html : (render "" {:pagination-numbers (paintings2.pagination/pagination-numbers)})
I want to display number which stands for pagenumbers. These are generated by the pagination-numbers function
I have this home.html page :
{% extends "base.html" %}
{% block content %}
{% for painting in data.paintings %}
<div class="post-masonry col-md-4 col-sm-6">
<div class="post-thumb">
<img src= {{ painting.tiles}} alt="">
<div class="title-over">
<h4><a href="#"> {{painting.title}} </a></h4>
</div>
<div class="post-hover text-center">
<div class="inside">
<i class="fa fa-plus"></i>
<span class="date">25 Jan 2084</span>
<h4><a href="#">Title one goes here</a></h4>
<p>Cum sociis natoque penatibus et magnis dis parturient</p>
</div>
</div>
</div>
</div> <!-- /.post-masonry -->
{% endfor %}
{% endblock %}
`which responds to this route :
(layout/render
"home.html" {:data {:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))
:current-page page-num}})))
I have this part :
"home.html" {:data {:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))
:current-page page-num
:pagination-numbers (pagination/pagination-numbers)}}))
which calls this function :
(ns paintings2.pagination)
(def pagination-numbers
[]
(let [page (data.current-page)
prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
num-pages 475
lower (max 1 (- page prevnext))
upper (inc (min num-pages (+ page prevnext)))]
(range lower upper)))
but now I see this error message :
Too many arguments to def, compiling:(paintings2/pagination.clj:3:1)
(ns paintings2.routes.home
(:require [paintings2.layout :as layout]
[compojure.core :refer [defroutes GET]]
[ring.util.http-response :as response]
[ :as io]
[compojure.route :refer [resources]]
[environ.core :refer [env]]
[paintings2.api-get :as api]
[paintings2.pagination :as pagination]
[clj-http.client :as client]
[clojure.spec :as s]))
java.lang.Exception: namespace 'paintings2.pagination' not found, compiling:(paintings2/routes/home.clj:1:1)
java.lang.Exception: namespace 'paintings2.pagination' not found, compiling:(paintings2/routes/home.clj:1:1)
at clojure.core$throw_if.invokeStatic(core.clj:5771)
at clojure.core$load_lib.invokeStatic(core.clj:5857)
at clojure.core$load_lib.doInvoke(core.clj:5832)
at clojure.lang.RestFn.applyTo(RestFn.java:142)
at clojure.core$apply.invokeStatic(core.clj:659)
at clojure.core$load_libs.invokeStatic(core.clj:5889)
at clojure.core$load_libs.doInvoke(core.clj:5873)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.core$apply.invokeStatic(core.clj:659)
at clojure.core$require.invokeStatic(core.clj:5911)
at clojure.core$require.doInvoke(core.clj:5911)
at clojure.lang.RestFn.invoke(RestFn.java:805)
at paintings2.routes.home$eval17751$loading__7732__auto____17752.invoke(home.clj:1)
at paintings2.routes.home$eval17751.invokeStatic(home.clj:1)
at paintings2.routes.home$eval17751.invoke(home.clj:1)
at clojure.lang.Compiler.eval(Compiler.java:6978)
at clojure.lang.Compiler.eval(Compiler.java:6967)
at clojure.lang.Compiler.load(Compiler.java:7430)
oke, that comes from here :
(defn home-page [page]
(let [page-num (s/conform ::page page)
url ""
options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
(if (s/invalid? page-num) 1 page-num)
(layout/render)
"home.html" {:data {:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))
:current-page page-num
:pagination-numbers (pagination/pagination-numbers)}}))
furthermore, even if it were being used as a namespace, it's not a valid var because it's missing the symbol, i.e. data.current-page/my-var
if I understand you right, delete the current-page part and make it a parameter of the pagination-numbers function ?
in clojure when you have a function that needs data from somewhere else, it's usually best to just pass in the data as an argument
#rads. I have now this :
(defn home-page [page]
(let [page-num (s/conform ::page page)
url ""
options {:as :json :query-params {:key (env :key) :format "json" :type "schilderij" :toppieces "True" :p page-num :ps 10}}]
(if (s/invalid? page-num) 1 page-num)
(layout/render)
"home.html" {:data {:paintings (-> (client/get url options)
(api/get-objectNumbers)
(api/fetch-paintings-and-images-front-page))
:pagination-numbers (pagination/pagination-numbers page-num)}}))
and
(ns paintings2.pagination)
(defn pagination-numbers
[page]
(let [prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
num-pages 475
lower (max 1 (- page prevnext))
upper (inc (min num-pages (+ page prevnext)))]
(range lower upper)))
lein is giving me a very weird error when i try to do anything:
Could not find artifact base:lein-template:jar:0.1.0-SNAPSHOT in clojars ( )
This could be due to a typo in :dependencies or network issues.
If you are behind a proxy, try setting the 'http_proxy' environment variable.
i’m not behind a proxy, and there aren’t any network issues as far as i can tellI have this :
(defn pagination-numbers
[page]
(let [prevnext (if (< page 6) (- 11 page) (if (> page 460 ) (- 11 (- 467 page)) 5))
num-pages 475
lower (max 1 (- page prevnext))
upper (inc (min num-pages (+ page prevnext)))]
(range lower upper)))
and on the prevnext line I see this error message :
clojure.lang.Keyword cannot be cast to java.lang.Number
When I do the pagination-numbers in repl then there is no problem ,
(pagination-numbers 5)
gives a outpute.g.
(let [page-num (s/conform ::page page)
_ (println page-num)
other (bindings)]
…)
@dominicm I have a clue. When I do that in the pagination function page seems to have this as content : :clojure.spec/invalid
what is wierd because this line
(if (s/invalid? page-num) 1 page-num)
is taking care that page-num is always a numyep, I have checked that before : this line is checking if page is a valid number :
page-num (s/conform ::page page)
and then here I check if pagenum is a number or invalid :
(if (s/invalid? page-num) 1 page-num)
What I tried to do is when a user uses localhost:3000 the request to the external api is containing the page-number 1
I have made this spec test to take care of it :
(defn ->long [s] (try (Long/parseLong s) (catch Exception _ ::s/invalid)))
(s/def ::page (s/and (s/conformer ->long) (s/int-in 1 471)))
(s/def ::optional-page (s/nilable ::page))
you are right. it's not going to work. I change the test to optional-page because page can be nill or a number and now I see a nullpointer exception
is there a better way to take care that page-num is always 1 or another number even if a user is using a non-valid-number or character
@roelof It could work without much altering. You just need to use your if in the let, e.g.
[page-num (s/conform page)
safe-page-num (if (s/invalid? page-num) 0 page-num)
…]
Then safe-page-num will be 0 if it's invalidNow I have this part :
<div class="social-icons">
<ul>
{% for number in data.pagination-numbers %}
<li> number </li>
{ % endfor %}
</ul>
</div>
I have now to make the urls : Is there some tag for the base or can I do
<a href = /page={{number}}
<a href=”?page={{number}}”>text</a>
should work
That will just add ?page=N
to the current URL.
@seancorfield not additive though. Worth noting, probably not relevant here though
@seancorfield thanks, I will implement that